结果值存储在数组或 json 中

Tanaphon Prayoonprasop

我试图解决它很长时间。

我想将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在循环前定义一个列表,然后resultmsg循环内的每个元素附加一个新元素,最后result在循环后打印列表:

result = []
for line in file:
   ...
   result.append(msg)

print(result)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章