简体   繁体   English

如何在 Python 中打印 JSON 数据

[英]How to print JSON data in Python

I'm trying to print a list of COVID cases for each country using https://covid.ourworldindata.org/data/owid-covid-data.json我正在尝试使用https://covid.ourworldindata.org/data/owid-covid-data.json打印每个国家/地区的 COVID 病例列表

Here is my code:这是我的代码:

import json

f = open('covidData.json')

data = json.load(f)

new_string = json.dumps(data, indent =2)

for i in new_string['AFG']:
        print(i)
f.close()

this only shows the data in "AFG" such as "continent", "location", etc. But I want to know how I can print "data" -> "new_cases"这仅显示“AFG”中的数据,例如“大陆”、“位置”等。但我想知道如何打印“数据”->“new_cases”

I'm just learning python, but is there a syntax similar to ['AFG'.'data'.'new_cases'] that I can use to print the cases?我只是在学习 python,但是是否有类似于['AFG'.'data'.'new_cases']语法可以用来打印案例?

Extending this to the whole JSON file, can I upgrade my code so that I do not have to specify each country ID, rather use a general ['country'.'data'.'new_cases'] format to read the data?将此扩展到整个 JSON 文件,我可以升级我的代码,以便我不必指定每个国家/地区 ID,而是使用通用['country'.'data'.'new_cases']格式来读取数据?

Please let me know if anyone has any suggestions如果有人有任何建议,请告诉我

this only shows the data in "AFG"这仅显示“AFG”中的数据

As shown, your code should return an error.如图所示,您的代码应返回错误。

new_string['AFG'] doesn't work because you cannot index a string by another string. new_string['AFG']不起作用,因为您不能用另一个字符串索引一个字符串。 In other words, json.dumps is not needed, data is a dictionary that you can index by a key, and you would have to iterate them one-by-one换句话说, json.dumps是不需要的, data是一个字典,你可以通过一个键索引,你必须一个接一个地迭代它们

eg例如

for country_code, info in data.items():
  for country_info in info['data']:
    print('{}\t{}\t{}'.format(country_info['date'], info['location'], country_info['new_cases']))

is there a syntax similar to ['AFG'.'data'.'new_cases'] that I can use我可以使用类似于 ['AFG'.'data'.'new_cases'] 的语法吗

Seems you want to use JSONPath , but I think the above solution is readable enough似乎您想使用JSONPath ,但我认为上述解决方案足够可读


Depending on your exact needs, using the CSV dataset in pandas would be easier to filter on and iterate over根据您的确切需求,在 pandas 中使用 CSV 数据集将更容易过滤和迭代

It isn't clear exactly what you want to do - the data spans a number of days.目前尚不清楚您想要做什么 - 数据跨越数天。

This code will show you a count of all the new cases for each country in the time period the data covers.此代码将向您显示数据涵盖的时间段内每个国家/地区的所有新病例数。

import json

with open('owid-covid-data.json') as data_file:
  covid_data = json.load(data_file)

for country in covid_data.keys():
  country_data = covid_data[country]
  location = country_data['location']
  cases_data = country_data['data']
  total_new_cases = 0
  for data in cases_data:
    total_new_cases += data.get('new_cases',0)

  print(f'{location} - {total_new_cases}')

This will list the new cases for each country by date.这将按日期列出每个国家/地区的新病例。

import json

with open('owid-covid-data.json') as data_file:
  covid_data = json.load(data_file)

for country in covid_data.keys():
  country_data = covid_data[country]
  location = country_data['location']
  cases_data = country_data['data']
  total_new_cases = 0

  for data in cases_data:
    new_cases = data.get('new_cases',0)
    report_date = data['date']
    print(f'{location} - {report_date} - {new_cases}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM