python - 使用python从csv替换和删除列

原文 标签 python python-3.x csv

这是我正在编写的代码

import csv
import openpyxl

def read_file(fn):
    rows = []

    with open(fn) as f:
        reader = csv.reader(f, quotechar='"',delimiter=",")
        for row in reader:
            if row:                     
                rows.append(row)
    return rows 


replace = {x[0]:x[1:] for x in read_file("replace.csv")}


delete = set( (row[0] for row in read_file("delete.csv")) )  


result = []

input_file="input.csv"
with open(input_file) as f:
    reader = csv.reader(f, quotechar='"')
    for row in reader:
        if row:
            if row[7] in delete:
                continue                                   
            elif row[7] in replace:

                result.append(replace[row[7]])   
            else:
                result.append(row)                       



with open ("done.csv", "w+", newline="") as f:
    w = csv.writer(f,quotechar='"', delimiter= ",")
    w.writerows(result)


这是我的文件:

input.csv:

c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13
"-","-","-","-","-","-","-","aaaaa","-","-","bbbbb","-",","
"-","-","-","-","-","-","-","ccccc","-","-","ddddd","-",","
"-","-","-","-","-","-","-","eeeee","-","-","fffff","-",","


这是一个13列的CSV。我只对第八和十一领域感兴趣。

这是我的replace.csv:

"aaaaa","11111","22222"


delete.csv:

ccccc


所以我在做的是将replace.csv的第一列(逐行)与input.csv的第八列进行比较,如果匹配,则将input.csv的第八列替换为replace.csv的第二列和第11列输入与replace.csv第三列的关系
对于delete.csv,它逐行比较两个文件,如果找到匹配项,则删除整行。
并且如果replace.csv或delete.csv中没有任何行,则按原样打印该行。
所以我想要的输出是:

c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13
"-","-","-","-","-","-","-",11111,"-","-",22222,"-",","
"-","-","-","-","-","-","-","eeeee","-","-","fffff","-",","


但是当我运行这段代码时,它会给我这样的输出:

c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13
11111,22222


我要去哪里错了?
我试图更改我之前发布过的问题的程序。由于输入文件已更改,因此我试图更改程序。
https://stackoverflow.com/a/54388144/9279313

最佳答案

@anuj
我认为SafeDev的解决方案是最佳选择,但是如果您不想使用熊猫,只需对代码做些改动即可。

for row in reader:
    if row:
        if row[7] in delete:
            continue                                   
        elif row[7] in replace:
            key = row[7]
            row[7] = replace[key][0]
            row[10]= replace[key][1]
            result.append(row)
        else:
            result.append(row)  


希望这能解决您的问题。

相关文章:

python - 当cat | python subprocess.check_output不返回grep组合[重复]

python - 遍历列表,同时增加另一个变量

python - 如何在不删除所有数据的情况下将列表转换为数据框?

encoding - Python 3中的流/字符串/字节数组转换

python - 文件流-ValueError:嵌入的空字节

php - 变量,循环不会增加

java - Java扫描仪问题,Notecard类

python - 使用Open CV用鼠标裁剪ROI时出现的问题

sql - PostgreSQL:仅当存在时CAST列

python - 在python中同时并行化不同的功能