使用Python的内联CSV文件编辑 [英] Inline CSV File Editing with Python

查看:109
本文介绍了使用Python的内联CSV文件编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用Python的CSV库或类似技术内嵌修改CSV文件吗?



当前我正在处理文件并更新第一列(名称字段)以更改格式。我的代码的简化版本看起来像这样:

  with open('tmpEmployeeDatabase-out.csv','w')as csvOutput:
writer = csv.writer(csvOutput,delimiter =',',quotechar ='')

with open('tmpEmployeeDatabase.csv','r')as csvFile:
reader = csv.reader(csvFile,delimiter =',',quotechar ='')

读取行:
row [0] = row [0] .title()
writer.writerow(row)

好奇,如果我可以做一个内联编辑,以便我不会重复的文件。



我试过以下,但这会附加新的记录到结束

 与open('tmpEmployeeDatabase.csv','r +')as csvFile:
reader = csv.reader(csvFile,delimiter =',',quotechar ='')
writer = csv.writer(csvFile,delimiter =',',quotechar ='')

for reader in reader:
row [1] = row [1] .title()
writer.writerow(row)


解决方案

不,您不应尝试写入您当前正在阅读的文件。如果您在读取行后保持查找,则可以执行此操作,但这不可取,特别是如果您要写回的数据超过了



标准方法是写入一个新的临时文件,并将其移动到您读取的旧文件上。

 来自tempfile import NamedTemporaryFile 
import shutil
import csv

filename = tmpEmployeeDatabase.csv'
tempfile = NamedTemporaryFile(delete = False)

with open(filename,'rb')as csvFile,tempfile:
reader = csv.reader(csvFile, delimiter =',',quotechar ='')
writer = csv.writer(tempfile,delimiter =',',quotechar ='')

b $ b row [1] = row [1] .title()
writer.writerow(row)

shutil.move(tempfile.name,filename)


我使用了 tempfile shutil 图书馆,使任务更容易。


Can I modify a CSV file inline using Python's CSV library, or similar technique?

Current I am processing a file and updating the first column (a name field) to change the formatting. A simplified version of my code looks like this:

with open('tmpEmployeeDatabase-out.csv', 'w') as csvOutput:
    writer = csv.writer(csvOutput, delimiter=',', quotechar='"')

    with open('tmpEmployeeDatabase.csv', 'r') as csvFile:
        reader = csv.reader(csvFile, delimiter=',', quotechar='"')

        for row in reader:
            row[0] = row[0].title()
            writer.writerow(row)

The philosophy works, but I am curious if I can do an inline edit so that I'm not duplicating the file.

I've tried the follow, but this appends the new records to the end of the file instead of replacing them.

with open('tmpEmployeeDatabase.csv', 'r+') as csvFile:
    reader = csv.reader(csvFile, delimiter=',', quotechar='"')
    writer = csv.writer(csvFile, delimiter=',', quotechar='"')

    for row in reader:
        row[1] = row[1].title()
        writer.writerow(row)

解决方案

No, you should not attempt to write to the file you are currently reading from. You can do it if you keep seeking back after reading a row but it is not advisable, especially if you are writing back more data than you read.

The canonical method is to write to a new, temporary file and move that into place over the old file you read from.

from tempfile import NamedTemporaryFile
import shutil
import csv

filename = 'tmpEmployeeDatabase.csv'
tempfile = NamedTemporaryFile(delete=False)

with open(filename, 'rb') as csvFile, tempfile:
    reader = csv.reader(csvFile, delimiter=',', quotechar='"')
    writer = csv.writer(tempfile, delimiter=',', quotechar='"')

    for row in reader:
        row[1] = row[1].title()
        writer.writerow(row)

shutil.move(tempfile.name, filename)

I've made use of the tempfile and shutil libraries here to make the task easier.

这篇关于使用Python的内联CSV文件编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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