Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts a Google Location JSON file into a CSV with Timestamp, Latitude, and Longitude.
import re
import json
import csv
JSON_data="C:/Users/JSON FILE PATH.txt"
CSV_file="C:/Users/CSV FILE PATH.csv"
with open(JSON_data) as f:
data = json.load(f)
csv_f=open(CSV_file, 'w')
for x in range(len(data["locations"])):
regex_Time=re.findall(".timestampMs.: .\d\d\d\d\d\d\d\d\d\d\d\d\d.", str(data["locations"][x]))
regex_Lat=re.findall(".latitude...: \d\d\d\d\d\d\d\d\d", str(data["locations"][x]))
regex_Long=re.findall(".longitude...: -\d\d\d\d\d\d\d\d\d", str(data["locations"][x]))
csv_f.write(regex_Time[0][16:-1] + "," + regex_Lat[0][14:] + "," + regex_Long[0][15:]+'\n')
csv_f.close
print('Done')
@troldmand

This comment has been minimized.

Copy link

@troldmand troldmand commented Sep 17, 2020

Hi! I just tried, but ran into this:

Traceback (most recent call last):
  File "script.py", line 16, in <module>
    csv_f.write(regex_Time[0][16:-1] + "," + regex_Lat[0][14:] + "," + regex_Long[0][15:]+'\n')
IndexError: list index out of range

Is it a 'known issue' or did I likely just do something wrong?

@troldmand

This comment has been minimized.

Copy link

@troldmand troldmand commented Sep 17, 2020

It's this one, that doens't work:
regex_Long[0][15:]

@troldmand

This comment has been minimized.

Copy link

@troldmand troldmand commented Sep 17, 2020

Ah! We found (my girlfriend and I), that there's a typo in l. 15: remove the dash before the /d's

regex_Long=re.findall(".longitude...: -\d\d\d\d\d\d\d\d\d", str(data["locations"][x]))

should be
regex_Long=re.findall(".longitude...: \d\d\d\d\d\d\d\d\d", str(data["locations"][x]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.