Python:csvwriter的问题 [英] Python: problems with csvwriter

查看:208
本文介绍了Python:csvwriter的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

看起来像这样:


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

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 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

推荐答案

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

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

希望这可以帮助你

这篇关于Python:csvwriter的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆