我有一个 list l1=['hi','hello',23,1.23]
.
我想写文字['hi','hello',23,1.23]
在文本文件中,即列表的值。
简单
with open('f1.txt','w') as f:
f.write(l1)
不行,所以我尝试了这个
f=open('f1.txt','w')
l1=map(lambda x:x+',', l1)
f.writelines(l1)
f.close()
但这也行不通。它说
TypeError: unsupported operand type(s) for +: 'int' and 'str'
。
当列表包含数字、字母和 float 时如何实现此目的?
最佳答案
您刚刚:
with open('f1.txt', 'w') as f:
f.write(str(l1))
关于python - 在 python 中编写一个文本文件中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47990399/