-
Notifications
You must be signed in to change notification settings - Fork 7
/
json2csv.py
50 lines (40 loc) · 1.89 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
#####################################################################################
# Python script to convert Google Maps JSON file to a CSV file
# Copyright (C) 2017 Dimitri Souza
# https://github.com/dmrsouza/json2kml
#####################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License v3 as published by
# the Free Software Foundation.
#####################################################################################
import json
import csv
import sys
import codecs
inputFile = "Saved Places.json"
outputFile = "Saved Places.csv"
# JSON Encoding is UTF-8. Change stdout to UTF-8 to prevent encoding error
# when calling print titles inside the loop
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
print ('Opening input file "'+inputFile+'"')
with open (inputFile) as jsonFile:
data = json.load (jsonFile)
print ('Creating output file "'+outputFile+'"')
with open(outputFile, 'wt') as csvfile:
csvWriter = csv.writer(csvfile,quoting=csv.QUOTE_ALL,lineterminator='\n')
csvWriter.writerow(['Latitude', 'Longitude', 'Name', 'Address'])
count = 0
for place in data["features"]:
if place["geometry"]["type"] == "Point":
title = place["properties"]["Title"]
print ('Parsing place "'+title+'"')
placeLocation = place["properties"]["Location"]
lon = place["geometry"]["coordinates"][0]
lat = place["geometry"]["coordinates"][1]
if "Address" in placeLocation:
address = placeLocation ["Address"]
else:
address = "N/A"
csvWriter.writerow ([lat,lon,title,address])
count += 1
print ('Done! Total of '+str(count)+' places saved to the CSV file.')