Python:csvwriter 的问题

发布于 2024-09-29 17:34:56 字数 710 浏览 0 评论 0原文

我正在尝试将数据(主要是日期、布尔值和浮点数据类型)写入 CSV 文件格式。这是我的代码片段:

# Write data to file
with open(OUTPUT_DIR + output_filename,'w') as outputfile:
    wrtr = csv.writer(outputfile, delimiter=',', quotechar='"')

    for x, y in datarows.items():
        a,b,c,d,e,f,g = (somedate.strft('%Y-%m-%d'),0,6058.7,False,1913736200,0,False)                     
        rowstr = "{0},{1},{2},{3},{4},{5},{6}".format(a,b,c,d,e,f,g)
        wrtr.writerow(rowstr)

    outputfile.close()

文件内容如下所示:

2,0,0,7,-,10,-,03,",",0,",",6,0,5,8,.,7,",",F,a,l ,s,e,",",1,9,1,3,7,3,6,2,0,0,",",0,",",F,a,l,s,e

我目前正在使用原始文件对象写入文件 - 但我更喜欢使用 csvwrite - 因为这就是它的用途

I am attempting to write data (mostly dates, booleans and float data types) into a CSV file format. Here is a snippet of my code:

# Write data to file
with open(OUTPUT_DIR + output_filename,'w') as outputfile:
    wrtr = csv.writer(outputfile, delimiter=',', quotechar='"')

    for x, y in datarows.items():
        a,b,c,d,e,f,g = (somedate.strft('%Y-%m-%d'),0,6058.7,False,1913736200,0,False)                     
        rowstr = "{0},{1},{2},{3},{4},{5},{6}".format(a,b,c,d,e,f,g)
        wrtr.writerow(rowstr)

    outputfile.close()

File contents look like this:

2,0,0,7,-,10,-,03,",",0,",",6,0,5,8,.,7,",",F,a,l,s,e,",",1,9,1,3,7,3,6,2,0,0,",",0,",",F,a,l,s,e

I am currently using the raw file object to write to file - but I would prefer to use the csvwrite - since that is what its supposed to be used for

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2024-10-06 17:34:56

尝试使用 wrtr.writerow([a,b,c,d,e,f,g]) 来代替。

writerow() 参数必须是值列表,而不是文本行。 csv.writer 的想法是,它将根据 CSV 规则格式化行值。

您得到的结果是由于 writerow() 将您的 rowstr 字符串解释为字符列表。

Try wrtr.writerow([a,b,c,d,e,f,g]) instead.

writerow() parameter must be a list of values, not a text line. The idea of csv.writer is that it will format row values according to CSV rules.

The result you have is due to the fact that writerow() interpreted your rowstr string as a list of characters.

谁与争疯 2024-10-06 17:34:56

看看这个例子也许它可以帮助你:

import datetime

with open('file.csv','w') as outputfile:
    wrtr = csv.writer(outputfile, delimiter=',', quotechar='"')
    a = (datetime.datetime.now().strftime('%Y-%m-%d'),0,6058.7,False,1913736200,0,False)
    wrtr.writerow(a)  # pass an iterable here

    # outputfile.close() you don't have to call close() because you use with

file.csv

2010-11-01,0,6058.7,False,1913736200,0,False

希望这可以帮助你

look at this example maybe it can help you:

import datetime

with open('file.csv','w') as outputfile:
    wrtr = csv.writer(outputfile, delimiter=',', quotechar='"')
    a = (datetime.datetime.now().strftime('%Y-%m-%d'),0,6058.7,False,1913736200,0,False)
    wrtr.writerow(a)  # pass an iterable here

    # outputfile.close() you don't have to call close() because you use with

file.csv

2010-11-01,0,6058.7,False,1913736200,0,False

hope this can help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文