如何在csv文件python中的同一行写所有项目

时间:2017-05-18 16:31:18

标签: python

我的代码是这样的:

F = open('words.csv', 'w') 
F.write(words + n1 + n2 )

这里的单词,n1,n2是项目列表。

但CSV文件中的输出如下:

Hi 6
1376
Hello 39
697
Nihao 30
396

我的问题是如何在同一行上写下所有项目,例如,

Hi 6 1376
Hello 39 697
Nihao 30 396

谢谢。

3 个答案:

答案 0 :(得分:1)

只需n1来自F = open('words.csv', 'w') F.write(words + n1.rstrip() + n2 ) 的<{1}}:

>>> n = "6\n"
>>> n.rstrip()
'6'
>>> 
<a id="toggleit-1" href="javascript:showonlyone('hiddendiv-1');">Link #1</a> 
<a id="toggleit-2" href="javascript:showonlyone('hiddendiv-2');">Link #2</a>
<a id="toggleit-3" href="javascript:showonlyone('hiddendiv-3');">Link #3</a>
<a id="toggleit-4" href="javascript:showonlyone('hiddendiv-4');">Link #4</a>

答案 1 :(得分:0)

您可以在csv库中使用import csv with open('words.csv', 'wb') as csvfile: csvwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) rows = zip(words, n1, n2) for row in rows: csvwriter.writerow(row)

"C:\Users\username\Desktop\Package\system\blender\blender.exe" "C:\Users\username\Desktop\Package\system\version_data\CurrentVersion.blend" -P "C:\Users\username\Desktop\Package\system\version_data\BlenderScript.py" "C:\Users\username\Desktop\Package\input_data\\" -y

答案 2 :(得分:0)

tempList = zip(words, n1, n2)
for line in tempList:
    F.write(reduce(lambda x, y: str(x)+" "+str(y), line))