Python:将数组转换为文本文件

Python:将数组转换为文本文件,python,text-files,Python,Text Files,我以以下格式处理了这些数据: x = [1,2,3,4,5,6] 等等。如何将此列表转换为.txt文件 Python 2.7,每行生成一个数字: with open('list.txt', 'w') as f: print >> f, '\n'.join(str(xi) for xi in x) 您可以使用任何其他连接字符串,如“”、“”,在一行中生成以逗号分隔的数字。Python 2.7,在每行中生成一个数字: with open('list.txt', 'w') as

我以以下格式处理了这些数据:

x = [1,2,3,4,5,6]

等等。如何将此列表转换为.txt文件

Python 2.7,每行生成一个数字:

with open('list.txt', 'w') as f:
    print >> f, '\n'.join(str(xi) for xi in x)

您可以使用任何其他连接字符串,如“
”、“
”,在一行中生成以逗号分隔的数字。

Python 2.7,在每行中生成一个数字:

with open('list.txt', 'w') as f:
    print >> f, '\n'.join(str(xi) for xi in x)

您可以使用任何其他连接字符串,如“
”、“
”,在一行中生成逗号分隔的数字。

在python中,您可以使用命令写入文件
write()
将字符串的内容写入缓冲区。不要忘记使用
close()
函数关闭文件

data = [1,2,3,4,5,6]

out = open("output.txt", "w")

for i in data:
    out.write(str(i) + "\n")

out.close()

在python中,可以使用命令写入文件
write()
将字符串的内容写入缓冲区。不要忘记使用
close()
函数关闭文件

data = [1,2,3,4,5,6]

out = open("output.txt", "w")

for i in data:
    out.write(str(i) + "\n")

out.close()
123456
保存到txt中

with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+',')
with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+'\n')
1,2,3,4,5,6,
保存到txt中

with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+',')
with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+'\n')
将节省

1
2
3
4
5
6
转换成txt

123456
保存到txt中

with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+',')
with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+'\n')
1,2,3,4,5,6,
保存到txt中

with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+',')
with open(r'C:\txtfile\exported_array.txt', 'w+') as txt_export:
    for i in x: txt_export.writelines(str(i)+'\n')
将节省

1
2
3
4
5
6

进入txt

您希望最终的文本文件是什么样子?每行一个号码?或逗号分隔的数字(或单词等)列表?是否要稍后将数据恢复回Python列表?您希望最终的文本文件是什么样的?每行一个号码?或逗号分隔的数字(或单词等)列表?是否要稍后将数据恢复回Python列表?关闭前无需刷新。在文件操作的新代码中首选
。关闭前无需刷新。在文件操作的新代码中,首选使用<代码>和。