我有一个包含网址列表的文本文件。鉴于每个网址都是一条直线:" https://www.amazon.com/George-Foreman-GRP1060B-Serving-Removable/dp/B00KDVJLJW" " https://www.amazon.com/ThermoPro-Wireless-Digital-Kitchen-Thermometer/dp/B014DAVCP4"
以下是代码:
linedata = []
with open("url.list") as f:
for line in f:
linedata = line.split('/')
Title = '/'.join(linedata[3:4])
Asin = ''.join(linedata[5]) + ''
f = open("url.text","a")
for list in linedata:
f.write(str(Asin))
f.write('\n')
f.close
代码部分有效。循环不起作用的原因?如何在同一行写入Title和asin文件?我正在运行python 2.7并提前感谢。
答案 0 :(得分:0)
(假设url.list每行有1个url)这是你应该做的:
linedata = []
reqData = []
with open("url.list") as f:
for line in f:
linedata = line.split('/')
reqData.append(linedata[3]+' : '+linedata[5].split('"')[0])
f = open("url.text","a")
for data in reqData:
f.write(str(data))
f.write('\n')
f.close()