假设我有两个列表: a=[1,2,3] b=[4,5,6] 我想将它们写入一个文本文件,以便获得一个两列文本文件:
1 4
2 5
3 6
最佳答案
只需 zip
列表,并将它们写入 csv 文件,并以制表符作为分隔符:
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> zip(a,b)
[(1, 4), (2, 5), (3, 6)]
>>> import csv
>>> with open('text.csv', 'w') as f:
... writer = csv.writer(f, delimiter='\t')
... writer.writerows(zip(a,b))
...
>>> quit()
$ cat text.csv
1 4
2 5
3 6
关于list - python : Write two lists into two column text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082718/