How to read and compare value of one csv with other csv in python?

What is the way to read a csv file and compare a field value in another other csv?

We are giving a complete code example of reading csv and picking value from one csv and reading other csv file to compare the value. You can implement any logic to perform on values of csv files. Below is the example of using core csv module instead of using external package/module of python:

import csv

#Open files

fb_csv = open('first.csv', newline='', encoding='utf-16')

sendgrid_csv  = open('second.csv', newline='', encoding='utf-8')

result_csv    = open('results.csv', 'w', newline='')

 

fb_reader       = csv.reader(fb_csv, delimiter='\t')

sendgrid_reader = csv.reader(sendgrid_csv, delimiter=',')

result_writer   = csv.writer(result_csv)

 

#Row Header

result_writer.writerow(['Email'])

fb_reader = list(fb_reader)

# Exclude row header

fb_reader = fb_reader[1:]

sendgrid_reader = list(sendgrid_reader)

# Exclude row header

sendgrid_reader = sendgrid_reader[1:]

print(len(fb_reader))

print(len(sendgrid_reader))

for row in fb_reader:

    fb_email = row[12]

    found = False

    for srow in sendgrid_reader:

        sendgrid_email = srow[0]

        if fb_email.strip() == sendgrid_email.strip():

            found = True

            break

 

    if found == False:

        result_writer.writerow([fb_email])

 

#Close files

fb_csv.close()

sendgrid_csv.close()

result_csv.close()

 

print("------- Finished --------")