JOKER
JOKER

Reputation: 75

Compare all values in a column with another column in CSV Python

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.

Upvotes: 0

Views: 700

Answers (1)

Lumorti
Lumorti

Reputation: 180

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.

Upvotes: 1

Related Questions