for (k, v) in data_dict.items: AttributeError: 'list'对象没有属性“items”; --

发布于 2025-01-10 08:35:50 字数 835 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2025-01-17 08:35:50

由于您的 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':
                ...

Since your JSON file starts with a [ (as mentioned in the question comments), the contents of your file will be loaded (at json.load) as a list of dictionaries:

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':
                ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文