Python:csvwriter的问题

编程入门 行业动态 更新时间:2024-10-28 09:25:31
本文介绍了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的问题

本文发布于:2023-05-25 10:50:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   csvwriter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!