I'm trying to make a python script that writes a given number of JSON objects to a text file. Every object needs to be on its own line.
I want to be able to loop X amount of times and every time I iterate through that loop I want to write a JSON Object to a utf8 encoded text file. Each JSON object will be on its own line.
import json
count = 5
while(count > 0):
data = {
"Address":[{
"Street":"Main St",
"State":"WY"
}],
"Name":"Jim",
"LastName":"Beam"
}
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
count = count -1;
This keeps overwriting the entire file so it ends up overwriting the file 5 times over.
I want the file to look like this:
{"Address":[{"Street":"Main St", "State":"WY"}], "Name":"Jim", "LastName":"Beam"}
{"Address":[{"Street":"Main St", "State":"WY"}], "Name":"Jim", "LastName":"Beam"}
{"Address":[{"Street":"Main St", "State":"WY"}], "Name":"Jim", "LastName":"Beam"}
{"Address":[{"Street":"Main St", "State":"WY"}], "Name":"Jim", "LastName":"Beam"}
{"Address":[{"Street":"Main St", "State":"WY"}], "Name":"Jim", "LastName":"Beam"}