While making persistent API calls, I am looping over a large list in order to reorganize my data and save it to a file, like so:
for item in music:
# initialize data container
data = defaultdict(list)
genre = item[0]
artist= item[1]
track= item[2]
# in actual code, api calls happen here, processing genre, artist and track
data['genre']= genre
data['artist'] = artist
data['track'] = track
# use 'a' -append mode
with open('data.json', mode='a') as f:
f.write(json.dumps([data], indent=4))
NOTE: Since I have a window of one hour to make api calls (after which token expires), I must save data to disk on the fly, inside the for loop
.
The method above appends data to data.json
file, but my dumped lists are not comma separated and file ends up being populated like so:
[
{
"genre": "Alternative",
"artist": "Radiohead",
"album": "Ok computer"
}
]
[
{
"genre": "Eletronic",
"artist": "Kraftwerk",
"album": "Computer World"
}
]
So, how can I dump my data ending up with a list of lists separated by commas?