-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcity_geo_json_to_csv.py
44 lines (41 loc) · 1.3 KB
/
city_geo_json_to_csv.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
import csv
import json
###########################################################
# city_geo_json_to_csv.py
#
# Purpose:
# convert city_geo.json to city_area.csv
#
# Prerequisites:
# city_geo.json exits under the same directory with this file
#
# Output: city_area.csv
# [stateID, stateName, countyID, countyName, cityID, cityName, landArea, waterArea]
#
# Example:
# python city_geo_json_to_csv.py
#
# Author:
# Qiushi Bai ([email protected])
###########################################################
f = open("city_geo.json",)
data = json.load(f)
cities = data["features"]
with open("city_area.csv", "w") as csv_out:
csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
header = ["stateID", "stateName", "countyID", "countyName", "cityID", "cityName", "landArea", "waterArea"]
csv_writer.writerow(header)
for city in cities:
properties = city["properties"]
row = [
properties["stateID"],
properties["stateName"],
properties["countyID"],
properties["countyName"],
properties["cityID"],
properties["name"],
properties["landArea"],
properties["waterArea"]
]
csv_writer.writerow(row)
print("wrote to csv file city_area.csv.")