for (k, v) in data_dict.items: AttributeError: '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]
I am trying to use the json data which is in data_dict and use it in the for loop to segregate the json data into different attributes with the help of k and v. But I got struck with the above AttributeError, any help is much appreciated.
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的 JSON 文件以
[
开头(如问题注释中所述),因此文件的内容将作为字典列表加载(在json.load
处) :Since your JSON file starts with a
[
(as mentioned in the question comments), the contents of your file will be loaded (atjson.load
) as a list of dictionaries: