简体   繁体   English

使用Python编辑CSV文件的特定位置

[英]Editing specific locations of a CSV file with Python

I have a CSV file that looks as such: 我有一个看起来像这样的CSV文件:

header1, header2, header3, header4
1, 2, 3, abc
4, 5, 6, abc
7, 8, 9, abc

My goal is to only change the values marked as "abc". 我的目标是仅更改标记为“ abc”的值。 I want the file to look like this when I'm done: 完成后,我希望文件看起来像这样:

header1, header2, header3, header4
1, 2, 3, test
4, 5, 6, test
7, 8, 9, test

The code I have come up with so far is as follows: 到目前为止,我提出的代码如下:

import csv

with open('test2-2.csv', 'w') as csvout:
    write=csv.writer(csvout, delimiter=',', lineterminator='\r')
    with open('test2.csv', 'rb') as csvfile:
        read=csv.reader(csvfile, delimiter=',')
        for row in read:    
            row[3]="test"
            write.writenow(row)

The problem with this code is that it overwrites the "header4" location as well, so it comes out looking like this: 此代码的问题在于,它也会覆盖“ header4”位置,因此看起来像这样:

header1, header2, header3, test
1, 2, 3, test
4, 5, 6, test
7, 8, 9, test

Is there any way to specify the index locations I want to change? 有什么方法可以指定我要更改的索引位置?

Thanks for any help! 谢谢你的帮助!

Here is the new code that works with this example: 这是适用于此示例的新代码:

import csv

with open('test2-2.csv', 'w') as csvout:
    write=csv.writer(csvout, delimiter=',', lineterminator='\r')
    with open('test.csv', 'rb') as csvfile:
        read=csv.reader(csvfile, delimiter=',')
        header=next(read)
        write.writerow(header)
        for row in read:    
            row[3]="test"
            write.writerow(row)

If you're just looking for a way to treat the header row separately, that's pretty easy: Just process the first row separately. 如果您只是在寻找一种单独处理标题行的方法,那很简单:只需单独处理第一行。

with open('test2-2.csv', 'w') as csvout:
    write=csv.writer(csvout, delimiter=',', lineterminator='\r')
    with open('test2.csv', 'rb') as csvfile:
        read=csv.reader(csvfile, delimiter=',')
        header = next(read)
        write.writerow(header)
        for row in read:
            row[3]="test"
            write.writenow(row)

(Note that this still has the same bugs are your original code—at least one typo, writenow , a missing skipinitialspace=True , etc.) (请注意,这仍然与原始代码具有相同的错误-至少有一个错字, writenow ,缺少的skipinitialspace=True等)


If you want to change things by some different rule, just write a different transformation. 如果要通过某些不同的规则进行更改,只需编写不同的转换即可。 As long as you can describe it in English, you should be able to convert it to Python pretty easily. 只要您可以用英语描述它,就应该可以轻松地将其转换为Python。

For example, if you wanted to change any 6 in any column into test : 例如,如果要将任何列中的任何6更改为test

for row in read:
    row = ('test' if col == '6' else col for col in row)
    write.writenow(row)

Or, if you wanted to change column 2 of row 3 into test : 或者,如果您想将第3行的第2列更改为test

for i, row in enumerate(read):
    if i == 3:
        row[2] = 'test'
    write.writenow(row)

Or, if you wanted to change any 6 in column 2 of any row: 或者,如果您想更改任何行的第2列中的任何6

for i, row in enumerate(read):
    if row[2] == '6':
        row[2] = 'test'
    write.writenow(row)

… and so on. … 等等。

Just wrap your replacing code with an if block, and use enumerate() (see #added comments): 只需将替换代码包装在if块中,然后使用enumerate() (请参阅#added注释):

import csv

with open('test2-2.csv', 'w') as csvout:
    write=csv.writer(csvout, delimiter=',', lineterminator='\r')
    with open('test2.csv', 'rb') as csvfile:
        read=csv.reader(csvfile, delimiter=',')
        for row in enumerate(read): #modified
            if row[0] >= 1: #added
                row[1][3]="test"
                write.writenow(row)

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

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