簡體   English   中英

如何通過將其與python中的其他csv文件進行比較來刪除和替換csv文件中的列?

[英]How to delete and replace columns in a csv file by comparing it to other csv files in python?

我正在編寫一個python代碼來搜索,刪除和替換csv文件中的列,我有3個文件。

Input.csv:

aaaaaaaa,bbbbbb,cccccc,ddddddd
eeeeeeee,ffffff,gggggg,hhhhhhh
iiiiiiii,jjjjjj,kkkkkk,lllllll
mmmmmmmm,nnnnnn,oooooo,ppppppp
qqqqqqqq,rrrrrr,ssssss,ttttttt
uuuuuuuu,vvvvvv,wwwwww,xxxxxxx

delete.csv:

aaaaaaaa
eeeeeeee
uuuuuuuu

replace.csv:

iiiiiiii,11111111,22222222
mmmmmmmm,33333333,44444444

這是我的代碼:

input_file='input.csv'
new_array=[]
for line in open(input_file):
    data=line.split(',')
    a==data[0]
    b=data[1]
    c=data[2]
    d=data[3]
    for line2 in open(delete):
        if (name in line2)==True:
            break
        else:
            for line1 in open(replace):
                data1=line1.split(',')
                aa=data1[0]
                replaced_a=data1[1]
                repalced_b=data1[2]


            if (data[0]==data1[0]):

                data[0]=data1[1]
                data[2]=data1[2]
                new_array=data
                print(new_array)

            else:   
                new_array=data

我的邏輯是:

1)open input.csv read line by line
2)load elements into an array
3)compare first element with entire delete.csv
4)if found in delete.csv then do nothing and take next line in array
5)if not found in delete.csv then compare with replace.csv
6)if the first element is found in the first column of replace.csv then replace the element by the corresponding second column of replace.csv and the second element with the corresponding 3rd third column of repalce.csv.
7)load this array into a bigger 10 element array.

所以我想要的輸出是:

11111111,22222222,kkkkkk,lllllll
33333333,44444444,oooooo,ppppppp
qqqqqqqq,rrrrrr,ssssss,ttttttt

所以現在我面臨以下問題:1)replace.csv或delete.csv中不存在的行無法打印2)我的input.csv有可能在一個條目中包含換行符,因此按行讀取行是一個問題,但是可以確定分布在不同行上的數據是在引號之間。 例如:

aaaaa,bbbb,ccccc,"ddddddddddd
ddddddd"
11111,2222,3333,4444

我們對將代碼和我的邏輯結合在一起的任何幫助表示贊賞。

我建議對此進行一些更改:

  • 在字典中閱讀要replace的內容
    • 將鍵設置為數據第0個位置,將值設置為替換數據第0和第1個位置的內容
  • 將要delete的內容讀入一組
    • 如果您的數據行以它開頭:跳過行,否則將其添加到輸出中。

遍歷您的數據,並使用這兩個查找來“做正確的事”。

我對您的數據進行了一些更改,以合並提到的“轉義的”數據,包括換行符:

文件創建:

with open("i.csv","w") as f: 
    f.write("""
aaaaaaaa,bbbbbb,cccccc,ddddddd
eeeeeeee,ffffff,gggggg,hhhhhhh
iiiiiiii,jjjjjj,kkkkkk,lllllll
"mmmm
mmmm",nnnnnn,oooooo,ppppppp
qqqqqqqq,rrrrrr,ssssss,ttttttt
uuuuuuuu,vvvvvv,wwwwww,xxxxxxx""")

with open ("d.csv","w") as f: 
    f.write("""
aaaaaaaa
eeeeeeee
uuuuuuuu""")

with open ("r.csv","w") as f: 
    f.write("""
iiiiiiii,11111111,22222222
"mmmm
mmmm",33333333,44444444""")

程序:

import csv

def read_file(fn):
    rows = [] 
    with open(fn) as f:
        reader = csv.reader(f, quotechar='"',delimiter=",")
        for row in reader:
            if row:                     # eliminate empty rows from data read
                rows.append(row)
    return rows 

# create a dict for the replace stuff        
replace = {x[0]:x[1:] for x in read_file("r.csv")}

# create a set for the delete stuff
delete = set( (row[0] for row in read_file("d.csv")) )  

# collect what we need to write back
result = []

# https://docs.python.org/3/library/csv.html
with open("i.csv") as f:
    reader = csv.reader(f, quotechar='"')
    for row in reader:
        if row:
            if row[0] in delete:
                continue                                   # skip data row
            elif row[0] in replace:
                # replace with mapping, add rest of row
                result.append(replace[row[0]] + row[2:])   # replace data
            else:
                result.append(row)                         # use as is

# write result back into file
with open ("done.csv", "w", newline="") as f:
    w = csv.writer(f,quotechar='"', delimiter= ",")
    w.writerows(result)

檢查結果:

with open ("done.csv") as f:
    print(f.read()) 

輸出:

11111111,22222222,kkkkkk,lllllll
33333333,44444444,oooooo,ppppppp
qqqqqqqq,rrrrrr,ssssss,ttttttt

Doku:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM