I'm trying to learn Python and I got stuck on something.
I'm looping through a dictionary and I'm printing out keys and their associated dictionary values.
I now want to take this output from my loop and instead of printing it to the screen, I need to write it to a text file.
here is an exmple from the web-
its value is numneric and not a string.
Here is my loop that correctly prints my dictionary elements and values.
I need help in how I can redirect the output to a text file.
Delg
I'm looping through a dictionary and I'm printing out keys and their associated dictionary values.
I now want to take this output from my loop and instead of printing it to the screen, I need to write it to a text file.
here is an exmple from the web-
with open('datafile1.txt', 'a') as f: f.write('\n') f.write('monkey') f.write('\n') f.write(str(15))I understand how this works, but how do I do the same for my loop? To further complicate things, I don't know if any of my data will throw an error because
its value is numneric and not a string.
Here is my loop that correctly prints my dictionary elements and values.
I need help in how I can redirect the output to a text file.
print("\nThis prints the key and its associated values") for person in data['returnData']: print(f"Elid: {person['elid']}, Last Name: {person['name']}")Thanks for taking a look.
Delg
buran write Jul-11-2023, 03:41 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Attached Files
json_parser_1.py (Size: 25.76 KB / Downloads: 121)