Is there a function in python that allows us to save a list in a txt file and keep its format?
If I have the list:
values = ['1','2','3']
can I save it to a file that contains:
'['1','2','3']'
So far I print parts of the list in the terminal and copy those in to a txt file.
Python read file into list. To read a file into a list in Python, use the file. read() function to return the entire content of the file as a string and then use the str. split() function to split a text file into a list.
First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.
Try this, if it helps you
values = ['1', '2', '3'] with open("file.txt", "w") as output: output.write(str(values))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With