Python - 使用csv writer后csv文件为空

发布于 2024-09-26 22:25:34 字数 779 浏览 3 评论 0原文

这里是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 技术交流群。

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

发布评论

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

评论(5

英雄似剑 2024-10-03 22:25:34

我对 csv 模块不太熟悉,但这看起来确实更像是文件 IO 问题,而不是 csv 问题。

您在文件中看不到任何内容的原因是 python 仍然打开该文件。你需要关闭它。

因此,不要这样做:

spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|')

而是这样做:

f = open('eggs.csv', 'w')
spamWriter = csv.writer(f, delimiter=' ', quotechar='|')
# the rest of your code
f.close()

现在您应该在 Eggs.csv 中看到您想要的内容

希望这会有所帮助

I'm not too familiar with the csv module, but this does look like a file IO problem more than a csv 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:

spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|')

Do this instead:

f = open('eggs.csv', 'w')
spamWriter = csv.writer(f, delimiter=' ', quotechar='|')
# the rest of your code
f.close()

Now you should see what you want in eggs.csv

Hope this helps

随心而道 2024-10-03 22:25:34

这有点晚了,但我在单个评论之外还没有看到一个解决方案,那就是使用 withas。在这种情况下,它可能看起来像:

import csv     
with csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|') as spamWriter:   
    spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

我过去使用过这个,没有任何问题。

This is a bit late to the party, but a solution I have yet to see outside of a single comment is using with and as. In this case, it may look like:

import csv     
with csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|') as spamWriter:   
    spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

I've used this in the past with no problems.

尸血腥色 2024-10-03 22:25:34

该代码有效。您确定您的操作系统不仅仅对 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 else more eggs.csv from the command line.

顾冷 2024-10-03 22:25:34

您发布的代码非常适合我。

您确定运行该脚本的目录实际上是您正在检查的目录吗? (你每次尝试之前都会删除“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?)

小嗷兮 2024-10-03 22:25:34

我在 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.

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