我希望将列表文件写入csv文件:
a = ['0.18 man::-1_-1king|new\n', '0.19 gif::-2manchester\n', '0.177 united\n']
我该怎么办?
这是我的代码:
import csv
a = ['0.18 king\n', '0.19 manchester\n', '0.177 united\n']
with open("output.csv",'wb') as resultFile:
wr = csv.writer(resultFile)
wr.writerows(a)
我希望我的结果是这样的:
Column 1 Column 2
0.18 man::-1_-1king|new
0.19 gif::-2manchester
0.177 united
答案 0 :(得分:-1)
如果它是示例中所示的简单列表,则无需导入csv,只需尝试一下
a = ['0.18 king\n', '0.19 manchester\n', '0.177 united\n']
with open("output.csv",'wb') as fin:
for row in a:
word1, word2 = row.split()
fin.write('%s,%s\n'%(word1,word2))
a = ['0.18 man::-1king\n', '0.19 gif::-2manchester\n', '0.177 united\n']
with open("output.tsv",'wb') as fin:
for row in a:
word1, word2 = row.split()
fin.write('%s\t%s\n'%(word1,word2))