Closed rakeshk1987 closed 11 months ago
I haven't run an export in a while, it looks like the script is failing when looking for the key 'latitudeE7'.
Check to make sure the json object still has that key. Try commenting out line 15 and see if you get a new error.
Lastly make sure your are running the .py file from the correct location so it knows where to find the Takeout/Location History/Semantic Location History
folder.
I was able to fix the issue by myself. For some data, the lat and lon was missing. I have added a Try Catch block to skip these records
import os
import csv
import json
import datetime
def process_file(file_path, data_writer):
with open(file_path, encoding='utf-8-sig') as file:
data = json.load(file)
for obj in data['timelineObjects']:
if 'placeVisit' in obj:
try:
name = obj['placeVisit']['location']['name']
except KeyError:
if obj['placeVisit']['location'].get('address') is None:
name = 'Missing'
else:
name = obj['placeVisit']['location']['address']
try:
lat = obj['placeVisit']['location']['latitudeE7'] / 10**7
except KeyError as e:
lat = 'Missing'
try:
lon = obj['placeVisit']['location']['longitudeE7'] / 10**7
except KeyError as e:
lon = 'Missing'
try:
timestamp = obj['placeVisit']['duration']['startTimestamp']
except KeyError as e:
timestamp = 'Missing'
try:
address = obj['placeVisit']['location']['address']
except KeyError as e:
address = 'Missing'
try:
placeid = obj['placeVisit']['location']['placeId']
except KeyError as e:
placeid = 'Missing'
# Convert the timestamp to a datetime object
try:
datetime_obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
except ValueError:
datetime_obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
# Convert the datetime object to Unix epoch time
epoch_time = int(datetime_obj.timestamp())
# Write the data to the CSV file
try:
data_writer.writerow([timestamp, lat, lon, address, placeid, name, epoch_time])
except UnicodeEncodeError as e:
data_writer.writerow([timestamp, lat, lon, 'missing',placeid,'missing', epoch_time])
def main():
root_dir = "Takeout/Location History/Semantic Location History"
output_file = "semantic_location_history.csv"
with open(output_file, 'w', newline='') as outfile:
data_writer = csv.writer(outfile)
data_writer.writerow(["timestamp", "latitude", "longitude", "address", "placeid", "name", "epoch_time"])
for subdir, dirs, files in os.walk(root_dir):
for file in files:
if file.endswith(".json"):
file_path = os.path.join(subdir, file)
process_file(file_path, data_writer)
if __name__ == "__main__":
main()
print('\n',end="semantic_location_history.csv created in current directory" )
Closing the issue
File "C:\Users\rakes\OneDrive\Documents\Repos\google-takeout-location-parser\semantic_location_parser.py", line 15, in process_file lat = obj['placeVisit']['location']['latitudeE7'] / 10**7