簡體   English   中英

比較2個csv文件之間的列,並使用Python編寫差異

[英]Compare a column between 2 csv files and write differences using Python

我試圖通過比較2個csv文件之間的列來打印出差異。

CSV1:

SERVER,   FQDN,   IP_ADDRESS,  
serverA, device1.com, 10.10.10.1  
serverA,device2.com,10.11.11.1  
serverC,device3.com,10.12.12.1   
and so on..

CSV2:

FQDN, IP_ADDRESS, SERVER,  LOCATION  
device3.com,10.12.12.1,serverC,xx  
device679.com,20.3.67.1,serverA,we  
device1.com,10.10.10.1,serverA,ac  
device345.com,192.168.2.0,serverA,ad  
device2.com,192.168.6.0,serverB,af  
and so on...

我要做的是比較FQDN列並將差異寫入新的csv輸出文件。 所以我的輸出看起來像這樣:

Output.csv:

FQDN, IP_ADDRESS, SERVER, LOCATION  
device679.com,20.3.67.1,serverA,we  
device345.com,192.168.2.0,serverA,ad  
and so on..

我試過,但無法獲得輸出。

這是我的代碼,請告訴我哪里出錯了;

import csv

data = {}  # creating list to store the data

with open('CSV1.csv', 'r') as lookuplist:
 reader1 = csv.reader(lookuplist)
 for col in reader1:
    DATA[col[0]] = col[1]

with open('CSV2.csv', 'r') as csvinput, open('Output.csv', 'w', newline='') as f_output:
 reader2 = csv.reader(csvinput)
 csv_output = csv.writer(f_output)
 fieldnames = (['FQDN', 'IP_ADDRESS', 'SERVER'])
 csv_output.writerow(fieldnames)  # prints header to the output file

    for col in reader1:
     if col[1] not in reader2:
        csv_output.writerow(col)  

(編輯)這是我使用的另一種方法:

import csv

f1 = (open("CSV1.csv"))
f2 = (open("CSV2.csv"))

csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)

for col1, col2 in zip(csv_f1, csv_f2):
    if col2[0] not in col1[1]:
    print(col2[0])

基本上,在這里我只是試圖首先找出是否打印了不匹配的FQDN。 但它打印出整個CSV1列。 請幫助大家,很多研究已經進入了這個,但發現沒有運氣! :(

此代碼使用內置的difflib來吐出file1.csv中不出現在file2.csv ,反之亦然。

我使用Differ對象來識別行變化。 我假設您不會將換行視為差異,這就是我添加sorted()函數調用的原因。

from difflib import Differ
csv_file1 = sorted(open("file1.csv", 'r').readlines())
csv_file2 = sorted(open("file2.csv", 'r').readlines())
with open("diff.csv", 'w') as f:
    for line in Differ().compare(csv_file1,csv_file2)):
        dmode, line = line[:2], line[2:]
        if dmode.strip() == "":
            continue
        f.write(line + "\n")

請注意,如果行以某種方式不同(不僅在FQDN列中),它將出現在diff.csv

import csv

data = {}  # creating list to store the data

with open('CSV1.csv', 'r') as lookuplist, open('CSV2.csv', 'r') as csvinput, open('Output.csv', 'w') as f_output:
 reader1 = csv.reader(lookuplist)
 reader2 = csv.reader(csvinput)
 csv_output = csv.writer(f_output)
 fieldnames = (['FQDN', 'IP_ADDRESS', 'SERVER', 'LOCATION'])
 csv_output.writerow(fieldnames)  # prints header to the output file
 _tempFqdn = []
 for i,dt in enumerate(reader1):
     if i==0:
         continue
     _tempFqdn.append(dt[1].strip())
 for i,col in enumerate(reader2):
     if i==0:
         continue
     if col[0].strip() not in _tempFqdn:
         csv_output.writerow(col)
import csv

data = {}  # creating dictionary to store the data

with open('CSV1.csv', 'r') as lookuplist:
reader1 = csv.reader(lookuplist)
for col in reader1:
    data[col[1]] = col[1]  # stores the data from column 0 to column 1 in the data list

with open('CSV2.csv', 'r') as csvinput, open('Output.csv', 'w', newline='') as f_output:
reader2 = csv.reader(csvinput)
csv_output = csv.writer(f_output)
fieldnames = (['SERVER', 'FQDN', 'AUTOMATION_ADMINISTRATOR', 'IP_ADDRESS', 'PRIMARY_1', 'MHT_1', 'MHT_2',
               'MHT_3'])
csv_output.writerow(fieldnames)  # prints header to the output file

for col in reader2:
    if col[0] not in data:  # if the column 1 in CSV1 does not match with column 0 in CSV2 Extract
    col = [col[0]]


        csv_output.writerow(col)  # writes all the data that is matched in CMDB WLC Extract

基本上,我只需要在'for循環'下更改'not in'並更改數據列表中的列,這些列將從我正在創建的CSV1文件中讀取。

暫無
暫無

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

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