简体   繁体   中英

compare two different formats csv fils with python

I need to compare two different formats CSV files, the first file contains hostname, and IP address and the second file contains hostname, IP address, OS type, and uptime. the objective is to take the hostname from the first file and compare it with the hostname from the second file and if ok the code gives me OS type and uptime. this is my first test of code.

import sys

# f = sys.stdin
# If you need to open a file instead:
f_vm_srv = open('serveurs.csv')
f_srv_list =  open('fqdn-ip.txt')

for line1 in f_srv_list:
    hostname=line1.strip().split(";")
    for line in f_vm_srv:
        fields = line.strip().split(";")
        if fields[0] == hostname[0]:
            print (fields[12])

f_vm_srv.close()
f_srv_list.close()

but this code gives me just the first line of result.

Try changing your code to be:

import sys

# f = sys.stdin
# If you need to open a file instead:
f_vm_srv = open('serveurs.csv')
f_srv_list =  open('fqdn-ip.txt')

# read in the lines from the file
f_svr_list_lines = f_srv_list.readlines()
f_vm_srv_lines = f_vm_srv.readlines()

for line1 in f_svr_list_lines:
    hostname=line1.strip().split(";")
    for line in f_vm_srv_lines:
        fields = line.strip().split(";")
        if fields[0] == hostname[0]:
            print (fields[12])

f_vm_srv.close()
f_srv_list.close()

You could also look into reading you files in as Pandas DataFrames (see tha pandas read_csv function ).

If you are going to have to work a lot with csv in python and need to perform complex operations on then, then I strongly advise you get familiar with pandas .

You will be able to easily load your 2 files with read_csv and easily extract columns with using the ["column_name"] operator.

You can use the Pandas library, useful to read csv.

Suppose that file1 is the file that contains IP and hostname and the other file is file2 . Moreover, I suppose that both files are composed like:

IP_value;hostname_value //file1
IP_value;hostname_value;OStype_value;uptime_value //file2

So, I read the csv files:

import sys
import pandas as pd

df1 = pd.read_csv('file1.csv', sep=';', names=['ip', 'hostname'])
df2 = pd.read_csv('file2.csv', sep=';', names=['ip', 'hostname', 'ostype', 'uptime'])

Now, two Dataframe objects has been created. You iterate the df1 over df2 and if the ip of the df1 is equal to the ip of the df2 , you can do what you want.

Just a hint: you can access to the ip values using df1['ip'] !

for ip1 in df1['ip']:
    for ip2 in df2['ip']:
        if ip1 == ip2:
            //Your code

An alternative is to use the function where of the Numpy library or the function merge of the Pandas library.

In particular, the function merge is useful if you want to check equals values on a certain column. You will merge both Dataframes (in your case df1 and df2 ) in a new Dataframe ( df3 ):

df3 = pd.merge(df1, df2, on=['ip'], how='inner')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM