嘿,伙计们,这里的python新手,如果我运行以下代码:
test = [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
with open('listfile.txt', 'w') as file:
for item in test:
for i in range(2):
file.write("%s" % item)
file.write("\n")
文本文件如下所示:
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
关于如何使其看起来像这样的任何帮助:
a b c
a b c
a b c
a b c
提前感谢,请随时更正我的编码
使用^{}
您希望在每个项目之间有选项卡,而不是换行符。我更改的第二件事是在内部循环之后添加
file.write("\n")
,以便在每行之间有一条新行。最后,我添加了file.close()
来关闭文件代码将列表转换为带有
join
的字符串,然后通过使用\n
连接行来分隔行输出如下:
看起来您需要选项卡,因此可以使用
\t
而不是' '
加入:哪个输出:
相关问题 更多 >
编程相关推荐