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

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

问题描述

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

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)

推荐答案

不,您不应尝试写入当前正在读取的文件.如果您在读取一行后继续seek返回,您可以这样做,但这是不可取的,尤其是当您写回的数据多于读取的数据时.

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('w+t', newline='', delete=False)

with open(filename, 'r', newline='') 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)

我使用了 tempfileshutil 库在这里使任务更容易.

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

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

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