对于 data_dict.items 中的 (k, v):属性错误:'list'对象没有属性'items' --



我正试图使用data_dict中的json数据,并在for循环中使用它,在k和v的帮助下将json数据分隔成不同的属性。但我被上面的AttributeError所打动,任何帮助都将不胜感激。

with open('Yelp.json', 'r',encoding= 'utf8') as f: 
data_dict = json.load(f)
count = 0
for (k, v) in data_dict.items():
for values in v: 
#print()
for key in values:
if str(key) == 'business_id':
lsthrs[str(key)]=values[key] 
lstcat[str(key)]=values[key]
lstnbh[str(key)]=values[key]
lstatr[str(key)]=values[key]
lstgoodforatr[str(key)]=values[key]
lstparkingatr[str(key)]=values[key]
lstambienceatr[str(key)]=values[key]
tep = values[key]

由于JSON文件以[开头(如问题注释中所述(,文件的内容将作为字典列表加载(在json.load(:

with open('Yelp.json', 'r',encoding= 'utf8') as f:
data_list = json.load(f) # Since `data_dict` is actually a list
count = 0
# To find the keys of each object, you could use this print:
# print(data_list[0].keys())
for values in data_list:  # Remember that `values` is a dict
for key in values:  # The keys of the `values` dict
if str(key) == 'business_id':
...

相关内容

  • 没有找到相关文章

最新更新