Reputation: 71
import codecs, json
line, aktualny_workout, workout_data = None
def czytaj_workout(_line):
aktualny_workout = codecs.open(_line.strip(), 'r', 'utf-8', 'ignore')
workout_data = json.load(aktualny_workout)
return
with codecs.open('./output.txt', 'r', 'utf-8', 'ignore') as f:
line = f.readline()
while line:
czytaj_workout(line)
line = f.readline()
location_dic = workout_data[0]['points']
print("latitude: ", latitude_dic['latitude'])
print("longitude: ", longitude_dic['longitude'])
print(workout_data[0]['comments']) # >>> KeyError: 'comments'
print([*workout_data]) # rzeczywiście, brakuje sekcji comments
# indeed, there is no 'comments' key in workout_data
#it's stunnin'
Because Endomondo Sport Tracker quits, they sends to users an archive with all stored data (on request). I want to build from it a pretty pdf file. Snipplet above reads filenames from list of Endomondo workouts and prints out some data. The problem is, it doesn't see any data after the 'points' tag/key. And there is tag/key 'comments' located. Could anyone explain me what happens?
Here is a part of data I work on:
[
{"name": "z Marianowa"},
{"sport": "CYCLING_TRANSPORTATION"},
{"source": "TRACK_MOBILE"},
{"created_date": "2012-07-12 15:16:18.0"},
.
.
.
{"descend_m": 180},
{"points": [
[
{"location": [[
{"latitude": 53.3775774},
{"longitude": 15.2635784}
]]},
{"altitude": 81.5},
{"timestamp": "Thu Jul 12 12:07:41 UTC 2012"}
],
[
{"location": [[
{"latitude": 53.377387},
{"longitude": 15.2634523}
]]},
{"altitude": 81.5},
{"distance_km": 0.022},
{"speed_kmh": 15.52},
{"timestamp": "Thu Jul 12 12:07:46 UTC 2012"}
],
]},
{"comments": [[
{"author": "Andrzej Kryński"},
{"created_date": "2012-07-12 15:41:54.0"},
{"text": "Lepiej jechać przez Pęzino i Ulikowo: mały ruch, prawie żaden więc bezpiecznie, dobra nawierzchnia i najlepsze osiągi na tym odcinku."}
]]}
]
Upvotes: 0
Views: 62
Reputation: 7806
Recommendations:
EDIT: After the json file was posted I found that the format was bad but it was completely json files not a file with json records. So the first thing to do was fix the format.
import json
from pathlib import Path
my_file = Path('./output.txt')
bad_data_format = json.loads(my_file.read_text())
workout_data = dict()
for x in bad_data_format:
workout_data.update(x)
bad_points_format = data.get('points')
# the same technique need to be used on bad_points_format as bad_data_format. I leave this to homework
print(f"comments: {workout_data.get('comments')}")
Upvotes: 1