使用Python按列名更新CSV文件

时间:2014-09-08 11:51:37

标签: python csv

我的csv文件如下:

product_name, product_id, category_id
book, , 3
shoe, 3, 1
lemon, 2, 4

我想通过使用python' s csv库提供列名来更新每行的product_id。

所以如果我通过了一个例子:

update_data = {"product_id": [1,2,3]}

然后csv文件应为:

product_name, product_id, category_id
book, 1, 3
shoe, 2, 1
lemon, 3, 4

2 个答案:

答案 0 :(得分:1)

您可以使用现有的dictiter按顺序搜索项目,例如:

import csv

update_data = {"product_id": [1,2,3]}
# Convert the values of your dict to be directly iterable so we can `next` them
to_update = {k: iter(v) for k, v in update_data.items()}

with open('input.csv', 'rb') as fin, open('output.csv', 'wb') as fout:
    # create in/out csv readers, skip intial space so it matches the update dict
    # and write the header out
    csvin = csv.DictReader(fin, skipinitialspace=True)
    csvout = csv.DictWriter(fout, csvin.fieldnames)
    csvout.writeheader()
    for row in csvin:
        # Update rows - if we have something left and it's in the update dictionary,
        # use that value, otherwise we use the value that's already in the column.
        row.update({k: next(to_update[k], row[k]) for k in row if k in to_update})
        csvout.writerow(row)

现在 - 假设每个新列值都转到行号,并且之后应该使用现有值。您可以将该逻辑更改为仅在现有值为空时(或您希望的任何其他条件)时使用新值。

答案 1 :(得分:0)

(假设您使用的是3.x)

Python在标准库中有一个CSV模块,可以帮助读取和修改CSV文件。

使用它我会找到您所在列的索引并将其存储在您创建的字典中。一旦找到它,只需将列表项弹出到每一行。

import csv

update_data = {"product_id": [None, [1,2,3]]}
#I've nested the original list inside another so that we can hold the column index in the first position.

line_no = 0 
#simple counter for the first step.

new_csv = [] 
#Holds the new rows for when we rewrite the file.

with open('test.csv', 'r') as csvfile:
    filereader = csv.reader(csvfile)

    for line in filereader:
        if line_no == 0:

            for key in update_data:
                update_data[key][0] = line.index(key) 
                #This finds us the columns index and stores it for us.

        else:

            for key in update_data:
                line[update_data[key][0]] = update_data[key][1].pop(0) 
                #using the column index we enter the new data into the correct place whilst removing it from the input list.

        new_csv.append(line)

        line_no +=1

with open('test.csv', 'w') as csvfile:
    filewriter = csv.writer(csvfile)

    for line in new_csv:
        filewriter.writerow(line)