我试图解决它很长时间。
我想将result
值作为一组数据存储在数组或 JSON 中。
请帮助查看下面的代码。我还是不太明白。
我想要的结果:
result = ["Price Milk 80","Price Pen 30","Price Tomato 40","Price Banana 40"]
在for
循环中使用并将所有数据作为一组发送。
我当前的结果是下面的结果,打印到控制台:
['Price Milk 80']
['Price Pen 30']
['Price Tomato 40']
['Price Banana 40']
import requests
import json
import locale
import csv
file = csv.reader(open('file.csv'), delimiter=',')
for line in file:
hd = line[0]
url = line[1]
name = line[2]
headers = {
'Referer': hd
}
request = requests.get(url, headers=headers).json()
con_str = str(request["item"]["price"])
total_price = con_str[:-5]
msg = (name + " " + total_price)
result = [(msg)]
print(result)
尝试result
在循环前定义一个列表,然后result
为msg
循环内的每个元素附加一个新元素,最后result
在循环后打印列表:
result = []
for line in file:
...
result.append(msg)
print(result)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句