0

I am trying to compare a CSV file of 2 columns with 1st column having 1500 values and 2nd column having 900.

Example:

ValueA ValueB
ValueB ValueC
ValueD ValueA
Valuec ValueD
ValueF
ValueG
ValueH
ValueZ

The output logic is Take a value from 1st column and compare it with all values in 2nd column:

  1. If there is a match, do nothing
  2. If there is no match, output that Value to a file named results.csv

I am very new to programming and I have been looking at sites for this specific logic but failed to find.

Really appreciate any help on this. Thanks in advance.

0

1 Answer 1

1

First, the best thing to do would be to load everything into two different arrays using the inbuilt Python CSV library, like so:

import csv
leftCol = []
rightCol = []
with open('example.csv') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        if len(row) > 0:
            leftCol.append(row[0])
        if len(row) > 1:
            rightCol.append(row[1])

You then have the two columns stored in nice arrays, leftCol and rightCol. Then to compare them:

 for leftItem in leftCol:
      for rightItem in rightCol:
          if leftItem != rightItem:
              print(leftItem)

In this case it just prints it, but you can swap the print for a file write or something else.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Lumorti This helped. I wanted the not equal items, so in the if statement I changed the == to !=.
Oops, sorry, misread that bit! Glad it helped though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.