Python - 使用csv writer后csv文件为空
这里是Python新手。我试图解决在更大的程序中编写 csv 文件的问题,并决定回到基础知识来尝试找到问题。
我从 Python csv 读写文档中运行了一个精确的代码示例:
import csv
spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|')
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
当我进入工作目录并单击“eggs.csv”时,文件为空,报告为“0 kb”。我的较大程序(空 csv 文件)中也发生了同样的事情。我是否遗漏了一些完全明显的东西?
谢谢你!
编辑:
我刚刚尝试将代码修改为:
import csv
csvOut=open("eggs.csv", "wb")
spamWriter = csv.writer(csvOut, delimiter=' ', quotechar='|')
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
csvOut.close()
这有效。我不确定为什么第一个对我不起作用。
Python newbie here. I was trying to troubleshoot an issue with writing a csv file in a larger program and decided to go back to basics to try to find the problem.
I ran an exact code example from the Python csv reading and writing documention:
import csv
spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|')
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
When I go to my working directory and click on "eggs.csv" the file is empty is reported as being "0 kb". This same thing was happening in my larger program (empty csv files). Am I missing something completely obvious?
Thank you!
EDIT:
I just tried modified the code to:
import csv
csvOut=open("eggs.csv", "wb")
spamWriter = csv.writer(csvOut, delimiter=' ', quotechar='|')
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
csvOut.close()
And this worked. I am not sure why the first doesn't work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我对 csv 模块不太熟悉,但这看起来确实更像是文件 IO 问题,而不是 csv 问题。
您在文件中看不到任何内容的原因是 python 仍然打开该文件。你需要关闭它。
因此,不要这样做:
而是这样做:
现在您应该在 Eggs.csv 中看到您想要的内容
希望这会有所帮助
I'm not too familiar with the
csv
module, but this does look like a file IO problem more than acsv
problem.The reason that you see nothing in the file is that python still has the file open. You need to close it.
So rather than doing this:
Do this instead:
Now you should see what you want in eggs.csv
Hope this helps
这有点晚了,但我在单个评论之外还没有看到一个解决方案,那就是使用
with
和as
。在这种情况下,它可能看起来像:我过去使用过这个,没有任何问题。
This is a bit late to the party, but a solution I have yet to see outside of a single comment is using
with
andas
. In this case, it may look like:I've used this in the past with no problems.
该代码有效。您确定您的操作系统不仅仅对 CSV 大小进行四舍五入吗——毕竟,它只有几个字节!
您可以尝试
print(open("eggs.csv").read())
来查看文件是否实际上为空,或者使用命令more Eggs.csv
线。That code works. Are you sure your OS isn't just rounding down the CSV size -- after all, it will only be a few bytes!
You can try
print(open("eggs.csv").read())
to see whether the file is actually empty, or elsemore eggs.csv
from the command line.您发布的代码非常适合我。
您确定运行该脚本的目录实际上是您正在检查的目录吗? (你每次尝试之前都会删除“eggs.gsv”吗?)
The code you posted works perfectly for me.
Are you sure that the directory in which you run that script is actually the directory you're checking? (Do you delete "eggs.gsv" every time before you try it?)
我在 python 解释器中尝试了这个。在代码的第一段中,只有在退出 python 后(当文件关闭时),我才能看到 csv 中的内容。当然,这与关闭文件有关。
I tried this out in the python interpreter. In the first bit of code, I see content in the csv only after I exit python (when the file is closed). Definitely, this was something to with closing the file.