1

我正在尝试在 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 行和第 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

谢谢你的帮助 !

编辑
1more 示例它应该看起来像
第 1 行:car 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()
4

2 回答 2

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 得到的输出

在此处输入图像描述

于 2013-03-21T12:41:11.897 回答
1


Python 文档指出,对于函数csv.writer()...

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

... 然后 ...

fmtparams“可以给出其他可选的关键字参数来覆盖当前方言中的单个格式参数”。

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

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

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

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

您可以首先通过将列表的元素附加到time列表来实现所需的列分离bla,而不是将前者嵌套在后者中。然后您需要省略delimiter=' 'in 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

于 2013-03-21T14:02:31.753 回答