【问题标题】:find all matches of list1 file in list2 file, create files and write relevant output to them. Python在 list2 文件中查找 list1 文件的所有匹配项,创建文件并将相关输出写入它们。 Python
【发布时间】:2019-01-02 21:38:15
【问题描述】:

我有两个列表:

列表1:

http://some.com
http://thing.com
http://whatever.org

列表2:

http://www.totalywhatever.com/2018010110231/http://some.com
http://www.totalywhatever.com/2018012346789/http://some.com
http://www.totalywhatever.com/2018002378231/http://thing.com
http://www.totalywhatever.com/2018012346789/http://thing.com
http://www.totalywhatever.com/2018012110231/http://whatever.org
http://www.totalywhatever.com/2018012346789/http://whatever.org

我想为 list1 中的每一行创建单独的文件,并删除一些标志。例如:

http://some.com ,应该创建名为 ---> some.com 的文件 http://thing.com --> thing.com 等等……

对于这些文件,应将“list2”中包含相关短语的链接复制到(因此对于“some.com”,它应该是“list2”中的前两行)。

linux的等价物:

grep some.com list2 > some.com  # Maan.. how complex operations on files can be ?? 

当然它应该在所有项目的循环中......

我想出了这个。它 >> 几乎

with open('list1', 'r+') as out, open('list2') as list:
o = out.readlines()
l = list.readlines()
out.seek(0)
for o1 in o:
         for l1 in l:
                 if o1.find(l1) > 0:
                         with open(l1.replace('http://', "").replace('\\n', '').rstrip(), 'w') as plik:
                                plik.write(o1 + '\n')
                                plik.seek(0)
                                plik.close()

【问题讨论】:

    标签: python list find


    【解决方案1】:

    当我读到它时,我发现我应该在这一行写“a”而不是“w”:

    with open(l1.replace('http://', "").replace('\\n', '').rstrip(), 'w') as plik:
    

    希望对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      相关资源
      最近更新 更多