Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a list to a .txt file

Tags:

python

list

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.

like image 736
Luis Ramon Ramirez Rodriguez Avatar asked Nov 13 '15 05:11

Luis Ramon Ramirez Rodriguez


People also ask

How do I convert a list to a text file in Python?

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.

How do I save a TXT file in Python?

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.


1 Answers

Try this, if it helps you

values = ['1', '2', '3']  with open("file.txt", "w") as output:     output.write(str(values)) 
like image 112
Mangu Singh Rajpurohit Avatar answered Sep 23 '22 23:09

Mangu Singh Rajpurohit