Ivan Madolev
Ivan Madolev

Reputation: 119

Python script to compare two CSV files with same rows for new lines/differences

I have a python script with nmap to run monthly to check open ports.this is how the csv file looks like:

host    hostname    hostname_type   protocol    port    name    state    product    extrainfo   reason  version conf    cpe
82.214.228.176  176.228.214.82.in-addr.arpa PTR tcp 21  ftp open            syn-ack     3   
82.214.228.176  176.228.214.82.in-addr.arpa PTR tcp 22  ssh open            syn-ack     3   

Then when I run it again and will add another line if new port is open:

82.214.228.178  hnsmonitor.direcpceu.com    PTR tcp 443 https   open            syn-ack     3   

So can you help me with an example script to compare the old old.csv with the new.csv and print to csv file the new lines that are added?

Upvotes: 0

Views: 437

Answers (2)

zipa
zipa

Reputation: 27879

If you are open to using pandas this will do it:

import pandas as pd

old = pd.read_csv('old.csv', sep=';')
new = pd.read_csv('new.csv', sep=';')
final = new[~new.isin(old)].dropna()
final.to_csv('diff.csv', sep=';')

Upvotes: 1

nbirla
nbirla

Reputation: 610

old csv entries = list_a

new csv entries = list_b

These lists will have unique identifiers from the data in csv.

List item

Code to remove list_a items from list_b to get the new added entries:

for item in list_a:
    while list_b.count(item) > 0:
        list_b.remove(item)

Upvotes: 0

Related Questions