简体   繁体   English

Python 2.7-编辑CSV中的特定行,列

[英]Python 2.7 - Edit Specific Row,Column in CSV

I have a CSV with a bunch of data, and I need to change the data in a specific row and column. 我有一个包含大量数据的CSV,我需要在特定的行和列中更改数据。 I'm able to read through the file, find the row,col that I need to change, change the data, but I can't figure out how to commit that change to memory and re-write the CSV correctly. 我能够通读文件,找到需要更改的行,列,更改数据,但是我不知道如何将更改提交到内存并正确地重写CSV。

The following code does not fail, however it is not correct. 以下代码不会失败,但是不正确。 Instead of committing the changes that I make to the column (col), it just duplicates the row that I want to change. 而不是提交对列(col)所做的更改,它只是复制了我要更改的行。 The data is unchanged, and now I just have duplicate rows where I wanted changes. 数据没有变化,现在我只需要重复行。

import csv
import re
from tempfile import NamedTemporaryFile
import shutil

csvName = raw_input("Enter the filename: ")
tempfile = NamedTemporaryFile(delete=False)
newSize = 0
newArea = 0

with open(csvName, 'rb') as readCSV, tempfile:
    reader = csv.reader(readCSV)
    writer = csv.writer(tempfile)
    for row in reader:
        for col in row:
            if col.startswith('Connection:'):
                print col
            if col.startswith('Size:'):
                print "Current", col
                newSize = raw_input("Enter new size: ")
                newArea = int(newSize)*int(newSize)
                col = re.sub('[0-9]+', newSize, col)
                writer.writerow(row)
            if col.startswith('Area:'):
                col = re.sub('[0-9]+', str(newArea), col)
                writer.writerow(row)
        writer.writerow(row)

shutil.move(tempfile.name, csvName)

This is an example of one bit of data that needs to be changed: 这是需要更改的一点数据的示例:

Connection: D14Conn Type: B2B Size: 140 Geometry: Square Area: 19600 连接:D14Conn类型:B2B尺寸:140几何形状:正方形面积:19600

My code above will simply duplicate new rows into this data, and the changes I make to col are not committed - as below 我上面的代码将简单地将新行复制到该数据中,而我对col所做的更改未提交-如下所示

Connection: D14Conn Type: B2B Size: 140 Size: 140 Geometry: Square Area: 19600 Area: 19600 连接:D14Conn类型:B2B大小:140大小:140几何形状:正方形面积:19600面积:19600

When you assign to col you are assigning to a copy of the col value in row. 当您分配给col您将分配给col值在行中的副本。 As you iterate through row , you need to either keep reference to the index of the value you want to change, and update row at that index so that when you call writer.writerow(row) you are actually writing with a changed object, or create a new_row with the desired columns. 在遍历row ,您需要保留对要更改的值的索引的引用,并在该索引处更新row ,以便在调用writer.writerow(row)时实际上是在使用更改的对象进行写操作,或者用所需的列创建一个new_row As for your duplicates, remove writer.writerow(row) calls from your if blocks, they are redundant. 至于重复项,请从if块中删除writer.writerow(row)调用,它们是多余的。

Your row isn't changed. 您的row未更改。 You need to make a new_row with your new column values and do writer.writerow(new_row) 您需要使用新的列值创建一个new_row并执行writer.writerow(new_row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM