-
-
Save gifguide2code/4f337564129c7879da3ebc0089c2cf21 to your computer and use it in GitHub Desktop.
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] + "," + regex_Lat[0] + "," + regex_Long[0]+'\n') | |
csv_f.close | |
print('Done') |
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]))
I ran into the same problem. I removed the dash but still had the problem. Any other hints for dealing with this. I am brand new to Python. I'd be happy to PayPal someone $20 bucks for successfully running this script on my JSON data.
Sorry for the late response troldmand and balwit! I was working with my data in mind when I wrote the script, and my JSON file only contains coordinates from North America. So my latitude (measured from the equator) is always positive, and my longitude (measured from the Prime Meridian in Great Britain) is always negative. That would explain why removing the dash was somewhat successful if you're hailing from somewhere on the other side of the Prime Meridian. It wouldn't work if you were trying anything south of the equator though, so I'm updating this a bit.
? in regex can be used to match 0 or 1 of whatever is immediately before it, so I'm thinking if we place a -? before the digit sequence, it'll catch a negative if it's there and nothing if it's not.
Hope that helps! Thanks for commenting--it would have never occurred to me that people from other parts of the globe would try it.
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?
Hello!
Working with my data ran into the same issue + some adjustments that I needes to made because my Records.json
Renamed values:
- latitude -> latitudeE7
- longitude -> longitudeE7
- timestampsMS -> timestamp & Unix time -> yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
It's this one, that doens't work:
regex_Long[0][15:]