-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path003-parse.py
executable file
·151 lines (134 loc) · 4.46 KB
/
003-parse.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
https://github.com/opendataby/city-dashboard/issues/53
Example:
"items": {
"68036": {
"address": "рядом с улица Максима Богдановича, 143",
"category": {
"icon": "6",
"id": "25",
"parent_id": "6"
},
"crm_create_at": "2017-04-01",
"crm_date_planned": "2017-04-03",
"date_create": "1 апреля 0:16",
"date_planned": "03.04.2017",
"href": "/problem/68036",
"id": "68036",
"lat": "53.92498779",
"lng": "27.57044029",
"organisation_id": "136",
"photo": {
"after": [],
"before": []
},
"rating": "0",
"status": "3",
"user": {
"id": "6635",
"last_name": "Евгений",
"middle_name": "",
"name": "Евгений"
}
},
"68037": {
Possible status values:
> findstr "status" *.json | cut -d":" -f 2,3,4 | "C:\\Program \
Files\\Dev\\Git\\usr\\bin\\sort.exe" -u
"status": "1",
"status": "3",
"status": "4",
"status": "5",
"status": "7",
"""
import csv
import json
import os
from collections import OrderedDict as odict
def fields(ob):
if type(ob) == dict:
for f in ob.keys():
print(f)
else:
for f in dir(ob):
if not f.startswith('__'):
print(f)
def json2csv(inname, outname):
data = json.load(open(inname, encoding='utf-8'))
with open(outname, 'w', encoding='utf-8') as outcsv:
#outcsv.write('# https://github.com/opendataby/city-dashboard/issues/53\n')
#outcsv.write('# \n')
names = [
'id',
'category',
'author',
'name',
'address',
'lat',
'lng',
'status',
'rating',
'opened',
'planned',
'organisation_id'
]
writer = csv.DictWriter(outcsv, fieldnames=names)
writer.writeheader()
for issue in data['items']:
item = data['items'][issue].copy()
#fields(item['category'])
created = item['date_create'].split()[2] # "1 апреля 0:16"
authorid = item['user']['id'] if item['user'] else 0
name = item['user']['name'].strip() if item['user'] else ''
try:
entry = odict([
('id', item['id']),
('category', '{parent_id}.{id}'.format(**item['category'])),
('author', authorid),
('name', name),
('address', item['address'].strip()),
('lat', item['lat']),
('lng', item['lng']),
('status', item['status']),
('rating', item['rating']),
('opened', item['crm_create_at']+" "+created),
('planned', item['crm_date_planned']),
('organisation_id', item['organisation_id']),
])
except TypeError:
print('Error: file {}, issue {}'.format(inname, item['id']))
raise
"""
"address": "рядом с улица Максима Богдановича, 143",
"crm_create_at": "2017-04-01",
"crm_date_planned": "2017-04-03",
"date_create": "1 апреля 0:16",
"date_planned": "03.04.2017",
"lat": "53.92498779",
"lng": "27.57044029",
"photo": {
"after": [],
"before": []
},
"rating": "0",
"status": "3",
"user": {
"id": "6635",
"last_name": "Евгений",
"middle_name": "",
"name": "Евгений"
}
"""
writer.writerow(entry)
#sys.exit()
if __name__ == '__main__':
#for name in ['003-in-2017-04-01.json']:
for name in os.listdir('.'):
# 003-in-2017-04-01.json
if name.startswith('003-in-') and name.endswith('.json'):
month = name[7:-5]
outname = 'opendata-115-%s.csv' % month
print('creating %s' % outname)
json2csv(name, outname)