我有两个列表:
列表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-> something.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()
答案 0 :(得分:0)
当我阅读它时,看到应该在行中写上“ a”而不是“ w”:
with open(l1.replace('http://', "").replace('\\n', '').rstrip(), 'w') as plik:
希望能帮助到某人!