Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

linux - Comparing CSV matching rows with Python

I have two CSVs containing only one column, each:

littleListIPs.csv:

10.187.172.140
10.187.172.141
10.187.172.142
10.187.172.143
10.187.172.144
10.187.172.145
10.187.172.154
10.187.172.155

(...)

-

BigListIPs.csv:

10.187.172.146
10.187.172.147
10.187.172.148
10.187.172.149
10.187.172.150
10.187.172.151
10.187.172.152
10.187.172.153
10.187.172.154
10.187.172.155

(...)

I need a script that will compare them and create a third file (output.csv), containing every single row from littleListIPs.csv, and a column that confirms if that IP exists on the BigListIPs.csv file, like in the following output (you can place ";" instead of "|"):

10.187.172.140 | Not present in BigListIPs.csv
10.187.172.141 | Not present in BigListIPs.csv
10.187.172.142 | Not present in BigListIPs.csv
10.187.172.143 | Not present in BigListIPs.csv
10.187.172.144 | Not present in BigListIPs.csv
10.187.172.145 | Not present in BigListIPs.csv
10.187.172.154 | Present in BigListIPs.csv
10.187.172.155 | Present in BigListIPs.csv

I have seen a similar case that was solved here in Stack (Python: Comparing two CSV files and searching for similar items), but I could not manipulate it well for my needs, even being a simpler case. Thanks for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Written in python 2.x, since that's what I have handy.

  • Load BigIp list into a set. Checking for in an array is O(n), checking for in a set is O(1).
  • use with to open the files, which is good practice and makes sure they were closed properly.

code:

#!/usr/bin/env python

import csv

little_ip_filename = "littleListIPs.csv"
big_ip_filename = "BigListIPs.csv"
output_filename = "results.csv"

# Load all the entries from BigListIPs into a set for quick lookup.
big_ips = set()

with open(big_ip_filename, 'r') as f:
    big_ip = csv.reader(f)
    for csv_row in big_ip:
        big_ips.add(csv_row[0])

# print big_ips

with open(little_ip_filename, 'r') as input_file, open(output_filename, 'w') as output_file:
    input_csv = csv.reader(input_file)
    output_csv = csv.writer(output_file)
    for csv_row in input_csv:
        ip = csv_row[0]
        status = "Present" if ip in big_ips else "Not Present"
        output_csv.writerow([ip, status + " in BigListIPs.csv"])

littleListIPs.csv:

10.187.172.140
10.187.172.141
10.187.172.142
10.187.172.143
10.187.172.144
10.187.172.145
10.187.172.154
10.187.172.155

BigListIPs.csv:

10.187.172.146
10.187.172.147
10.187.172.148
10.187.172.149
10.187.172.150
10.187.172.151
10.187.172.152
10.187.172.153
10.187.172.154
10.187.172.155

results.csv:

10.187.172.140,Not Present in BigListIPs.csv
10.187.172.141,Not Present in BigListIPs.csv
10.187.172.142,Not Present in BigListIPs.csv
10.187.172.143,Not Present in BigListIPs.csv
10.187.172.144,Not Present in BigListIPs.csv
10.187.172.145,Not Present in BigListIPs.csv
10.187.172.154,Present in BigListIPs.csv
10.187.172.155,Present in BigListIPs.csv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...