python中的csv的writerow在大多数情况下不起作用 [英] csv's writerow in python doesn't work most of the time
本文介绍了python中的csv的writerow在大多数情况下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些非常简单的代码:
I have some very simple piece of code:
import csv
out = csv.writer(open('test.csv', 'wb'), delimiter=",")
out.writerow([u"value1", u"value2", u"value3"])
有时可以正常工作,但大多数时候都无效!我记得我以前使用过writerow,现在工作得很好,我在这里遗漏了什么?
It sometimes works, but most of the time it doesn't! I remember I used writerow previously and it was working just fine, what am I missing here?
为了使问题更清楚:尽管执行了上面的代码而没有任何错误,但csv文件仍然为空
To make the problem clearer: the csv file remains empty although the above code is executed without any errors
推荐答案
我将代码修改如下:
import csv
f = open('test.csv', 'wb')
out = csv.writer(f, delimiter=",")
out.writerow([u"value1", u"value2", u"value3"])
f.close()
所以我所做的就是将open('test.csv', 'wb')
分离并将其分配给f
,以便最后可以使用f.close()
.这样可以很好地工作.
So all I've done is that I separated the open('test.csv', 'wb')
and assigned it to f
, so that I can use f.close()
at the end. It works perfectly this way.
这篇关于python中的csv的writerow在大多数情况下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文