【问题标题】:get all values for key in json list file python获取json列表文件python中键的所有值
【发布时间】:2019-01-23 03:13:02
【问题描述】:

我无法遍历 JSON 文件来获取键的所有值。我已经尝试了多种编写方法,但有很多错误。

# Import package
from urllib.request import urlretrieve

# Import pandas
import pandas as pd

# Assign url of file: url
url = 'https://data.sfgov.org/resource/wwmu-gmzc.json'

# Save file locally
urlretrieve(url, 'wwmu-gmzc.json')


# Loading JSONs in Python
import json
with open('wwmu-gmzc.json', 'r') as json_file:
    #json_data = json.load(json_file) # type list
    json_data = json.load(json_file)[0] # turn into type dict

print(type(json_data))

# Print each key-value pair in json_data
#for k in json_data.keys():
#    print(k + ': ', json_data[k])
for line in json_data['title']:
    print(line)
#w_title = json_data['title']
#print(w_title)
for key, value in json_data.items():
    print(key + ':', value)
    #print(json_data.keys('title') + ':' , jason_data['title']) 

这段代码的当前版本只给出了文件的第一行:

<class 'dict'> 1 8 0 release_year: 2011 actor_2: Nithya Menon writer: Umarji Anuradha, Jayendra, Aarthi Sriram, & Suba  locations: Epic Roasthouse (399 Embarcadero) director: Jayendra title: 180 production_company: SPI Cinemas actor_1: Siddarth actor_3: Priya Anand

更正下面的代码并解决缺少键的问题:

# Loading JSONs in Python
import json
with open('wwmu-gmzc.json', 'r') as json_file:
    content = json_file.read()
    json_data = json.loads(content)

print(type(json_data))

for json_i in json_data:
    try:
        print(json_i['locations'])
    except:
        print('***** NO KEY FOUND *****')

【问题讨论】:

    标签: python json python-3.x dictionary


    【解决方案1】:

    您只加载数据集中的第一个数据。

    with open('wwmu-gmzc.json', 'r') as json_file:
        json_data = json.load(json_file) # Input is list of dict. So,load everything
    
    for json_i in json_data:
        print(json_i.get('your_key', 'default_value'))
    

    【讨论】:

    • 这可行,但在某些键上出现“KeyError:'locations'”,所以我猜测数据很脏导致它。现在我必须做一些事情,以便当行中没有键时它会忽略/提醒。
    【解决方案2】:

    您的代码不起作用,因为您获取的数据实际上是一个列表。要读取列表中的每个项目(每个项目都是一个键值对),您可以这样做。

    # Import package
    from urllib.request import urlretrieve
    import json
    
    # Assign url of file: url
    url = 'https://data.sfgov.org/resource/wwmu-gmzc.json'
    
    # Save file locally
    urlretrieve(url, 'wwmu-gmzc.json')
    
    
    # Loading JSONs in Python
    with open('wwmu-gmzc.json', 'r') as json_file:
        content = json_file.read()
        json_data = json.loads(content)
    
    
    for item in json_data:
        print('======')
        for key, value in item.items():
            print(key + ':', value)
    
    

    【讨论】:

    • 我试过这个,但无法让它只给我特定键的值。谢谢。 ===== release_year : 2011 actor_2 : Nithya Menon 编剧 : Umarji Anuradha, Jayendra, Aarthi Sriram 和 Suba 地点 : Epic Roasthouse (399 Embarcadero) 导演 : Jayendra title : 180 production_company : SPI Cinemas actor_1 : Siddarth actor_3:Priya Anand ===== release_year:2011 年 actor_2:Nithya Menon 编剧:Umarji Anuradha、Jayendra、Aarthi Sriram 和 Suba 地点:Mason & California Streets (Nob Hill) 导演:Jayendr
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2018-12-18
    • 2016-06-20
    • 2020-10-13
    • 2018-11-06
    相关资源
    最近更新 更多