将列表保存到.txt文件 [英] Save a list to a .txt file
本文介绍了将列表保存到.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
python中是否有一个函数可以让我们将列表保存到txt文件中并保持其格式?
Is there a function in python that allows us to save a list in a txt file and keep its format?
如果我有列表:
values = ['1','2','3']
我可以将其保存到包含以下内容的文件中吗?
can I save it to a file that contains:
'['1','2','3']'
到目前为止,我在终端中打印列表的一部分,并将其复制到txt文件中.
So far I print parts of the list in the terminal and copy those in to a txt file.
推荐答案
如果有帮助,请尝试一下
Try this, if it helps you
values = ['1', '2', '3']
with open("file.txt", "w") as output:
output.write(str(values))
这篇关于将列表保存到.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文