I have JSON data which I am pulling in via API.
here's my code
# list of each api url to use
link =[]
#for every id in the accounts , create a new url link into the link list
for id in accounts:
link.append('https://example.ie:0000/v123/accounts/' id '/users')
accountReq = []
for i in link:
accountReq.append(requests.get(i, headers=headers).json())
with open('masterSheet.txt', 'x') as f:
for each in accountReq:
account = each['data']
for data in account:
list=(data['username'] " " " ",data['first_name'],data['last_name'])
f.write(str(list) "\n")
This pulls in data no problem . If I do
print(data['username'] " " " ",data['first_name'],data['last_name'])
I get all of the data back , around 500lines.
However my problem I am having is when I try to write to my file , it writes about 8lines of data and then stops running with no errors.
I'm assuming its due to the data size. How can I fix my issue of not printing all of the data to the .txt file??
CodePudding user response:
Are you trying to write each data point to the file? Your write function is outside the nested for loop, so you are actually only writing the last list
variable that you create to the file.
You should move the f.write() under the for loop if you intend to write every single data point into the file.
for data in account:
list=(data['username'] " " " ",data['first_name'],data['last_name'])
f.write(str(list) "\n")