Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何迭代文件并将所有id值放入列表[Python]_Python_Arrays_Json_Data Analysis - Fatal编程技术网

如何迭代文件并将所有id值放入列表[Python]

如何迭代文件并将所有id值放入列表[Python],python,arrays,json,data-analysis,Python,Arrays,Json,Data Analysis,我有这个文件,我想把ASIN的所有值放在自己的列表中,这是我到目前为止得到的,但我不知道如何做,或者最好的方法是什么,因为这个文件的扩展名是.json,但它的格式不是有效的json,所以我尝试像普通文本文件那样做 {'asin': '0756029929', 'description': 'Spanish Third-year Pin, 1 inch in diameter. Set of 10.', 'title': 'Spanish Third-year Pin Set of 10', '

我有这个文件,我想把ASIN的所有值放在自己的列表中,这是我到目前为止得到的,但我不知道如何做,或者最好的方法是什么,因为这个文件的扩展名是.json,但它的格式不是有效的json,所以我尝试像普通文本文件那样做

{'asin': '0756029929', 'description': 'Spanish Third-year Pin, 1 inch in diameter.  Set of 10.', 'title': 'Spanish Third-year Pin Set of 10', 'price': 11.45, 'imUrl': 'http://ecx.images-amazon.com/images/I/51AqSOl7qLL._SY300_.jpg', 'salesRank': {'Toys & Games': 918374}, 'categories': [['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Novelty', 'Clothing']]}
{'asin': '0756029104', 'description': 'Viva Espaol pin, 1 x 1 inch. Set of 10.', 'title': 'Viva Espanol Pins Set of 10', 'price': 11.45, 'imUrl': 'http://ecx.images-amazon.com/images/I/51By%2BZpF9DL._SY300_.jpg', 'salesRank': {'Home & Kitchen': 2651070}, 'categories': [['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Novelty', 'Clothing']]}
{'asin': '0839933363', 'description': 'This necklace from the popular manga and anime series, Death Note. The necklace\'s charm is black and silver with the text, "Death Note" upon it. The approx. length of the necklace is 12"Dimension & Measurement:Length: Approx. 12"', 'title': 'Death Note Anime Manga: Cross Logo necklace', 'imUrl': 'http://ecx.images-amazon.com/images/I/51f0HkHssyL._SY300_.jpg', 'salesRank': {'Toys & Games': 1350779}, 'categories': [['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Costumes & Accessories']]}
{'asin': '1304567583', 'description': 'pink bikini swimwear glow in the dark fashion', 'title': 'Pink Bikini Swimwear Glow in the Dark Fashion', 'price': 19.99, 'imUrl': 'http://ecx.images-amazon.com/images/I/41pS%2B98jhlL._SY300_.jpg', 'categories': [['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Costumes & Accessories', 'Costumes']]}
{'asin': '1304567613', 'description': 'Bikini Swimwear glow in the dark fashion blue', 'title': 'Bikini Swimwear Blue Glow in the Dark Fashion', 'price': 29.99, 'imUrl': 'http://ecx.images-amazon.com/images/I/41ZNIUvYkyL._SY300_.jpg', 'categories': [['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Costumes & Accessories', 'Costumes']]}
{'asin': '1465014578', 'title': '2013 Desk Pad Calendar', 'imUrl': 'http://ecx.images-amazon.com/images/I/51NadiHHHsL._SX342_.jpg', 'related': {'also_bought': ['B009SDBX0Q', 'B009DCUY1G'], 'bought_together': ['B009SDBX0Q', 'B009DCUY1G']}, 'salesRank': {'Clothing': 505645}, 'categories': [['Clothing, Shoes & Jewelry', 'Novelty, Costumes & More', 'Band & Music Fan', 'Accessories']]}
{'asin': '1620574128', 'related': {'also_bought': ['B0015KET88', 'B00004WKPP', 'B000F8T8U0', 'B000F8V736', 'B000F8VAOM', 'B0015KGFQM', 'B003U6P4OS', '1564519392', 'B000F8XF8Q', 'B0042SR3E2', 'B004PBLVDU', 'B000G3LR9Y', 'B0006PKZBI', 'B0007PC9CK', 'B001G98DS0', 'B001UFWJLW', 'B003S8XLWA', '0486214834', '1609964713', 'B000P1PVMQ', '0590308572', 'B000QDZY52', '1564514188', 'B0006PKZ7W', 'B000T2YKIM', 'B000QDTWF0', 'B000FA6DXS', 'B0007P94ZA', 'B000WA3FKU', 'B00004WKPU', 'B000F8XF68', 'B004DJ51JE', '

将列表理解与ast.literal\u eval一起使用:

with open('File.json', "r") as f:
    for line in f:
         if 'asin' in line:
        #Code that gets the values of asins
         clothing_ids.append(#then add them values to clothing_ids)

print(clothing_ids)

如果保证文件未被污染,您可以
eval
每行:

import ast
data = ast.literal_eval(open('File.json').read())
asins = [i['asin'] for i in data]

下面是相同的代码,使用@Ajax1234的
ast.literal\u eval()
,以避免受污染文件的问题,但使用我的列表理解,它单独计算每一行,因此不会将整个数据集存储为临时数据

with open('File.json', "r") as f:
    asins = [eval(line)['asin'] for line in f]

对于您的基于评论的奖金问题,获取所有“也购买”物品的列表,包括重复项:

import ast

with open('File.json', "r") as f:
    asins = [ast.literal_eval(line)['asin'] for line in f]

你可能想看看一个实际的JSON解析器。@MadPhysicator,但当我尝试这个方法时,由于文件的结构,我得到了一个错误,它抱怨说它没有可用的JSONTANKS!这成功了!但是你能给我解释一下它是如何工作的吗?每一行看起来都像一个有效的python表达式。
eval
函数将字符串解释为python代码,这样字符串
{'asin':'01234'}
将返回python
dict
对象。然后
['asin']
从字典中提取“asin”条目。
[…for line in f]
是标准的python列表理解。。。它在文件
f
中的所有行上循环,调用每一行
line
,生成列表。哦,当它工作时,不要使用它。使用@Ajax1234提供的
ast.literal\u eval
版本。。。或者,当有人污染了文件,并将实际代码放入其中时,您将执行他们的(可能是恶意的)代码。@AJNeufield该方法对我不起作用,尽管我得到的语法无效error@AJNeufield好的,谢谢你,最后一个问题,如果您再次查看我的数据文件,我如何在一个数组中一次获取一个项目的所有“还买”值?希望这是有意义的。当我尝试使用此.File“”第2行{'asin':'0681256036','related':{'allowed':['B005TLMG0M','B002R0DRR4','B005VQ0NBY','B001G5ZCBA','B00AI7VZTK','B0069S2DKW','B005TKZE1Q','B001G5ZCC4','B002R0DOSQ','B001PMUUPC','B005TL3RM8','B005TLNETE','B009O0H11I','B002C0F3RQ','B00AVQWCQI','B001HX4MCG','B00FF0VCAW','B00946ZOT8','B002WGHXI2'],“标题”:“暮光之城传奇:新月爱丽丝巧克力项链可穿戴道具复制品”,“价格”:17.59,,“salesRank”:{“玩具与游戏”:674790}]}^语法错误:无效syntax@Jake123词典本身包含无效的Python语法。在上面发布的示例中,最后一个括号(
]
)不是必需的。
import ast

also_bought = []
asins = []
with open('File.json', "r") as f:
    for line in f:
        item = ast.literal_eval(line)
        asins.append(item['asin'])
        if 'related' in item:
           related = item['related']
           if 'also_bought' in related:
              also_bought.extend(related['also_bought'])