在python中写入csv文件(文件在操作结束时留空) [英] Writing csv files in python (Files are left blank at the end of the operation)

查看:562
本文介绍了在python中写入csv文件(文件在操作结束时留空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python写csv,我想知道为什么我的输出文件是空白的,当程序完成运行。首先,这里是我的代码:

  reader = csv.reader(open(location,'rU'),delimiter =' ,',quotechar ='')
writer = csv.writer(open('temp.csv','wb'),delimiter =',',quotechar ='')

writer.writerow([test,test,test,test,test,test,test])

count = 0
对于读取器中的行:
如果count!= 0并且count< len(split):
writer.writerow([row [0],row [1],row [2],row [3],row [4],row [ ])
count = count + 1



我知道了几件事: p>


  • 程序完成后,写入temp.csv的所有内容都是测试行。

  • 如果我在运行过程中强行停止程序,则实际上文本正在写入文件:因为某种原因它被擦除了。

  • 我做了一些研究,很多人发现,关闭csv文件解决了他们的问题,但是这似乎并不适用于我,除非我缺少非常明显!



感谢您的帮助!



这里是一些更新的代码我正在使用:

  infile = open(location,'rU')
outfile = open('temp.csv','wb')
reader = csv.reader(infile,delimiter =',',quotechar ='')
writer = csv.writer(outfile,delimiter = ',',',quotechar ='')
split = email_text.split('NEW MESSAGE BEGINS HERE')

count = 0
for row in reader:
if count!= 0 and count< len(split):
writer.writerow([row [0],row [1],row [2],row [3],row [4],row [ ])
outfile.flush()
count = count + 1

infile.close()
outfile.close()


解决方案

您没有关闭文件。



使用上下文管理器:

如果你正在读文件p>

 打开(位置,'rU')为infile,打开('temp.csv','wb')为outfile:
reader = csv.reader(infile,delimiter =',',quotechar ='')
writer = csv.writer(outfile,delimiter =',',quotechar ='')
...

这将确保当 已退出(即使是由于异常)。


I am writing to csv in python, and I was wondering why my output files are blank when the program completes running. First off, here is my code:

reader = csv.reader(open(location, 'rU'), delimiter = ',', quotechar = '"')
writer = csv.writer(open('temp.csv', 'wb'), delimiter = ',', quotechar = '"')

writer.writerow(["test", "test", "test", "test", "test", "test", "test"])

count = 0
for row in reader:
    if count != 0 and count < len(split):
        writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], split[count-1]])
    count = count + 1

I know a few things about it:

  • When the program finishes, all that is written to 'temp.csv' is the "test" line. The rest of the document is empty.
  • If I force stop the program in the middle of running, there is in fact text being written to the file: it just gets wiped for some reason.
  • I've done some research, and lots of people have found that "closing" the csv file solves their problems, but that doesn't seem to work for me, unless I am missing something obvious!

Thanks in advance for your help!

Edit: here is some updated code I am working with:

infile = open(location, 'rU')
outfile = open('temp.csv', 'wb')
reader = csv.reader(infile, delimiter = ',', quotechar = '"')
writer = csv.writer(outfile, delimiter = ',', quotechar = '"')
split = email_text.split('NEW MESSAGE BEGINS HERE')

count = 0
for row in reader:
    if count != 0 and count < len(split):
        writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], split[count-1]])
        outfile.flush()
    count = count + 1

infile.close()
outfile.close()

解决方案

You're not closing the file. While this may work fine if you're just reading a file, it's definitely not a good idea to do this when writing a file.

Use a context manager:

with open(location, 'rU') as infile, open('temp.csv', 'wb') as outfile:
    reader = csv.reader(infile, delimiter = ',', quotechar = '"')
    writer = csv.writer(outfile, delimiter = ',', quotechar = '"')
    ...

This ensures that the files will be closed when the with block is exited (even if it's because of an exception).

这篇关于在python中写入csv文件(文件在操作结束时留空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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