Csv Writer - 尝试在每列中编写变量

时间:2013-03-21 12:36:26

标签: python csv writer

我试图在csv文件中写一些数据,但我不能选择不同的列..

car=["car 11"]
finish=["Landhaus , Nord"]
time=["['05:36']", "['06:06']", "['06:36']", "['07:06']", "['07:36']", "['08:06']", "['08:36']", "['09:06']", "['09:36']", "['10:06']", "['10:36']", "['11:06']", "['11:36']", "['12:06']", "['12:36']", "['13:06']", "['13:36']", "['14:06']", "['14:36']", "['15:06']", "['15:36']", "['16:06']", "['16:36']", "['17:06']", "['17:36']", "['18:06']", "['18:36']", "['19:06']", "['19:36']", "['20:06']", "['20:36']"]<br/>
myfile = open("Informationen.csv", "wb")
writer = csv.writer(myfile,dialect='excel',delimiter=' ')
bla =[car,finish,time]
writer.writerow(bla)

输出:

car 11 Landhaus , Nord "['05:36']", "['06:06']", [..]

全部在1行和Colum 1

但我希望它像这样

car 11 (in row 1 Colum 1) | "Landhaus , Nord" (in row 1 Column 2) | ['05:36'] (in Line 1 Column 3 ) | ['06:06'] (in row 1 Column 4 ) till Column n

感谢您的帮助!

编辑
再举一个例子它应该是什么样的 第1行:第11号车(第1栏)Landhaus,Nord(第2栏)['05:36'](第3栏)['05:36'](第4栏)[...] 例如http://img13.imageshack.us/img13/4964/unbenanntvilw.png

到目前为止的解决方案: 但是时间列表仍然存在问题

car=["car 11"]
trenn=[';']
finish=['Landhaus , Nord']
time=["['05:36']", "['06:06']", "['06:36']", "['07:06']", "['07:36']", "['08:06']", "['08:36']", "['09:06']", "['09:36']", "['10:06']", "['10:36']", "['11:06']", "['11:36']", "['12:06']", "['12:36']", "['13:06']", "['13:36']", "['14:06']", "['14:36']", "['15:06']", "['15:36']", "['16:06']", "['16:36']", "['17:06']", "['17:36']", "['18:06']", "['18:36']", "['19:06']", "['19:36']", "['20:06']", "['20:36']"]
myfile = open("Informationen2.csv", "wb")
writer = csv.writer(myfile,delimiter=' ')
bla = car + trenn + finish + trenn + time
writer.writerow(bla)
myfile.close()

2 个答案:

答案 0 :(得分:1)

import csv

car=["car 11"]
finish=['Landhaus , Nord']
time=["['05:36']", "['06:06']", "['06:36']", "['07:06']", "['07:36']", "['08:06']", "['08:36']", "['09:06']", "['09:36']", "['10:06']", "['10:36']", "['11:06']", "['11:36']", "['12:06']", "['12:36']", "['13:06']", "['13:36']", "['14:06']", "['14:36']", "['15:06']", "['15:36']", "['16:06']", "['16:36']", "['17:06']", "['17:36']", "['18:06']", "['18:36']", "['19:06']", "['19:36']", "['20:06']", "['20:36']"]
myfile = open("derp.csv", "wb")
writer = csv.writer(myfile)
bla = car + finish + time
writer.writerow(bla)
myfile.close()

这是我从excel获得的输出

enter image description here

答案 1 :(得分:1)

Python documentation表示csv.writer()功能...

  

可以给出用于定义的可选dialect参数   一组特定于特定CSV方言的参数“。

......那......

  

“其他可选的fmtparams关键字参数可以赋予覆盖当前方言中的各个格式参数”。

您遇到的问题是将time列表的字符串表示写入文件并使用空格分隔符写入文件的结果。如果您将Informationen.csv视为纯文本文件,问题就会变得明显。

首先,写入已通过空白分隔符作为csv.writer(myfile, dialect='excel', delimiter=' ')中的参数的文件会覆盖Excel方言中定义的默认分隔符,并导致列表bla的元素被写入文件中格式为element1 element2 element3,而不是element1,element2,element3

其次,尽管time列表中的大多数元素都根据需要在电子表格中分配了自己的列,但将列表作为字符串表示形式写入文件会导致整体不需要的格式化。 / p>

当您使用脚本作为Excel文件打开创建的文件时,Excel会根据它找到的前两个逗号读入两个初始值,这两个逗号恰好位于字符串'Landhaus , Nord'的中心和字符串中time列表的表示。

您可以通过将time列表的元素附加到bla列表来实现首先需要的列分隔,而不是将前者嵌套在后者中。然后,您需要在delimiter=' '中省略csv.writer(myfile, dialect='excel', delimiter=' '),从而在写入文件时避免使用分隔符覆盖效果:

import csv

car = ['car 11']
finish = ['Landhaus , Nord']
time = ["['05:36']", "['06:06']", "['06:36']", "['07:06']", "['07:36']"]
try:
    with open('Informationen.csv', 'w') as myfile:
        writer = csv.writer(myfile, dialect='excel')
        bla = [car, finish]
        for each_time in time:
            bla.append(each_time)
        writer.writerow(bla)
except IOError as ioe:
    print('Error: ' + str(ioe))

在Excel中生成以下输出:

http://imageshack.us/a/img839/8061/screenshotkn.png