【发布时间】:2021-03-15 23:18:47
【问题描述】:
我整天都在尝试将多个 JSON 文件从 nys.gov 网站上提取的有关 COVID 数据的多个 JSON 文件写入一个(或多个)CSV 文件。
我可以成功地连接 JSON 文件,但无法以我可以用来制作图表的格式将它们连接在一起。我知道问题出在我的 for 循环中,但经过多次尝试,我还没有找到一种成功的方法,可以将数据附加到一个 csv 或创建多个 csv 文件,然后我可以在 pandas 中使用。这是我的代码,目前它似乎正在遍历循环并将最终的 json 转储到我的 csv 中......
import csv
import datetime
import json
import pandas as pd
import urllib.request as request
one_day = datetime.datetime.today() - datetime.timedelta(days=1)
two_days = datetime.datetime.today() - datetime.timedelta(days=2)
thr_days = datetime.datetime.today() - datetime.timedelta(days=3)
for_days = datetime.datetime.today() - datetime.timedelta(days=4)
fiv_days = datetime.datetime.today() - datetime.timedelta(days=5)
six_days = datetime.datetime.today() - datetime.timedelta(days=6)
sev_days = datetime.datetime.today() - datetime.timedelta(days=7)
egt_days = datetime.datetime.today() - datetime.timedelta(days=8)
one_day_str = one_day.strftime("%Y-%m-%dT00:00:00.000")
two_day_str = two_days.strftime("%Y-%m-%dT00:00:00.000")
thr_day_str = thr_days.strftime("%Y-%m-%dT00:00:00.000")
for_day_str = for_days.strftime("%Y-%m-%dT00:00:00.000")
fiv_day_str = fiv_days.strftime("%Y-%m-%dT00:00:00.000")
six_day_str = six_days.strftime("%Y-%m-%dT00:00:00.000")
sev_day_str = sev_days.strftime("%Y-%m-%dT00:00:00.000")
egt_day_str = egt_days.strftime("%Y-%m-%dT00:00:00.000")
url_one = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + one_day_str
url_two = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + two_day_str
url_thr = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + thr_day_str
url_for = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + for_day_str
url_fiv = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + fiv_day_str
url_six = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + six_day_str
url_sev = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + sev_day_str
url_egt = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + egt_day_str
url_lst = [url_one,url_two,url_thr, url_for, url_fiv, url_six, url_sev, url_egt]
d = []
def write_json(data, filename='data.json'):
with open(filename,'w') as f:
json.dump(data, f, indent=4)
for url in url_lst:
with request.urlopen(url) as response:
# print(url)
source = response.read()
data = json.loads(source)
if len(data) == 0:
continue
with open ("covid.json", 'w') as outfile:
json.dump(data, outfile)
with open('covid.json') as json_data:
j = json.load(json_data)
d.append(j)
write_json(d)
filename = "County Stats.csv"
fields = ["Date","County", "New Positives", "All Positives", "New Tests", "All Tests"]
with open(filename, 'w') as fw:
cf = csv.writer(fw, lineterminator='\n')
# write the header
cf.writerow(fields)
for counties in data:
date = counties['test_date']
cnty = counties['county']
new_pos = counties['new_positives']
cum_pos = counties['cumulative_number_of_positives']
new_tests = counties['total_number_of_tests']
cum_tests = counties['cumulative_number_of_tests']
cf.writerow([date,cnty, new_pos, cum_pos, new_tests, cum_tests])
我可能介于 python 初学者和中级之间,所以请原谅任何糟糕的编码习惯。提前致谢。
- 康纳
【问题讨论】:
-
没有示例数据或文件,我们无能为力。
-
如果您运行代码,它将提取数据,它使用请求库来访问 nys.gov API
-
代码将创建文件 data.json、covid.json 和 County Stats.csv
-
在 data.json 文件中,您将找到我尝试聚合的 7 天数据。
-
您似乎多次将标题写入 CSV。
fields有六个元素,而数据行只有五个。我不知道这是否是您的问题。
标签: python json pandas api csv