以垂直格式python csv显示所有数据

时间:2013-10-29 05:59:24

标签: python excel csv

大家好,我尝试了很多显示以下代码的方法。我希望它以另一种方向显示。

此代码显示以下Excel文件。

newDirRH = "C:/Plots"
newfile = newDirRH + "/TabulatedStatsVSM.csv"
with open(newfile, "wb") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["NameIP", "TypeIP", "FieldIP", "SignalIP", "NameOP", "TypeOP", "FieldOP", "SignalOP"])
    writer.writerow(["name","type","[cm]","[m]","name","type","[cm]","[m]"])
    for field, signal, field1, signal1 in zip(FieldIP, signalIP, FieldOP, signalOP):
        writer.writerow([NameIP, TypeIP,field, signal, NameOP, TypeOP,field1, signal1])
        NameIP = TypeIP = NameOP = TypeOP =  ''

以下代码显示的Excel文件。

enter image description here

我正在努力实现这样的目标。可能吗?? 这个excel文件,我自己编辑。

enter image description here

2 个答案:

答案 0 :(得分:2)

你的问题:writer.writerow中没有“\ n”这就是你发现错误的原因。对于csv,您必须一次写一行。以下代码是您想要的。

import csv

FieldIP = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
FieldOP = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
signalIP = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.20]
signalOP = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.20]
NameIP = "JDP123"
TypeIP = "ID123"
NameOP = "JDP124"
TypeOP = "ID124"

newDirRH = "C:/VSMPlots"
newfile = newDirRH + "/TabulatedStatsVSM1.csv"
with open(newfile, "wb") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["NameIP", "TypeIP", "NameOP", "TypeOP"])
writer.writerow([NameIP, TypeIP, NameOP, TypeOP])
writer.writerow([" "])
writer.writerow(["FieldIP", "SignalIP", "FieldOP", "SignalOP"])
for field, signal, field1, signal1 in zip(FieldIP, signalIP, FieldOP, signalOP):
    writer.writerow([field, signal, field1,signal1])
print "Done"

答案 1 :(得分:0)

一次写作1,你应该没事。

writer.writerow(["NameIP", "TypeIP", "NameOP", "TypeOP"])
writer.writerow([NameIP, TypeIP, NameOP, TypeOP])
writer.writerow([" "]) # Leaving a space accordng to your excel
writer.writerow(["FieldIP", "SignalIP", "FieldOP", "SignalOP"])
for field, signal, field1, signal1 in zip(FieldIP, signalIP, FieldOP, signalOP):
    writer.writerow([field, signal, field1,signal1])