【发布时间】: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()
【问题讨论】: