-
Notifications
You must be signed in to change notification settings - Fork 10
/
json2csv.py
59 lines (53 loc) · 1.84 KB
/
json2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import time
import os
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)
try:
Area = dic[ 'data' ]['areaList']
except:
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']
try:
dict[ 'confirmed' ] = j[ 'confirmed' ]
dict[ 'suspected' ] = j[ 'suspected' ]
dict[ 'cured']=j['cured']
dict[ 'dead']=j['dead']
except:
dict[ 'confirmed']=j['confirmedCount']
dict[ 'suspected']=j['suspectedCount']
dict[ 'cured']=j['curedCount']
dict[ 'dead']=j['deadCount']
city.append(dict)
pd.DataFrame(city).to_csv('history/city_%s.csv'%time,index=0)
if __name__ == '__main__':
list=[]
for i in os.listdir('history/'):
a=i.split('_')[ 1 ]
list.append(a.split('.')[0])
for i in os.listdir('jsons/'):
if i.split('.')[0]!='latest':
j=timechange(i)
if j in list:
pass
else:
save_csv_area(i)