Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create json2csv.py #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions json2csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json
import time
import datetime
import pandas as pd

def timechange(timestamp):
timestamp=timestamp.split('.')[0]+timestamp.split('.')[1]
timestamp=int(timestamp[0:13])
# 转换成localtime
time_local = time.localtime(timestamp / 1000)
# 转换成新的时间格式(精确到秒)
dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
return dt

def save_csv_area(filename):
f = open("jsons/%s"%filename,'r',encoding='utf-8')
time=timechange(filename)
for line in f.readlines():
dic=json.loads(line)
Area = dic[ 'data' ]['listByArea']
city=[]
for i in Area:
for j in i['cities']:
dict = {}
dict[ 'provinceName' ] = i[ 'provinceName' ]
dict[ 'provinceAbbreviation' ] = i[ 'provinceShortName' ]
dict[ 'city']= j['cityName']
dict[ 'confirmed']=j['confirmed']
dict[ 'suspected']=j['suspected']
dict[ 'cured']=j['cured']
dict[ 'dead']=j['dead']
city.append(dict)

pd.DataFrame(city).to_csv('csv/city_%s.csv'%time,index=0)

if __name__ == '__main__':
save_csv_area('1579936450.7942688.json')