[Tutor] Writing a dictionary to txt file
Peter Otten
__peter__ at web.de
Sun Aug 30 03:59:48 EDT 2020
nzbz xx wrote:
> The original txt file is written in this format:
>
> Plant 1
> date & time
> description
> description
> description
> (Blank line)
> Plant 2
> ...
> ....
> ...
>
> I have converted this file into dict = {'Plant 1':
> ['datetime','description','description','description'], 'Plant2' :
> ['datetime',....]}. So how can i write this dict back to the txt file in
> the same format?
How about trying something yourself? Make an effort and write a few lines of
code. Here's a sketch:
Open a file for writing (use the with statement).
Iterate over the dict items (key-value pairs). Inside the loop:
Print the key. You may need to investigate how the print() function
works with files.
Iterate over the value list and print the items.
Print a blank line.
If your code doesn't work come back and post it here.
More information about the Tutor
mailing list