Python Forum

Full Version: Open previous csv file to read value under column for comparison
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I  need to check the value under the columns of two csv files (the current open file and the csv file written immediately before it) and if the value in the number column exists in the previous csv file, check to see if the value in the commits column is the same.  If they are different, do something (there are more columns in the csv than listed).  I'm having trouble trying to construct the correct comparison.  What I have now is minimal, not working and I'm stuck  Smile . Thanks for any help provided.

current.csv:
Number, Commits
1,              1,           
prevous.csv:
Number, Commits
1,             2

previous_csv = sorted(glob.iglob(reporting_path + '/' + '*.csv'), key=os.path.getctime)[-2]
with open(current_csv, 'r') as f:
      print(previous_csv)
Hello!
Just keep the values in a variable and when read the second .csv compare them
Thanks for the reply. That's the part I can't quite figure out is how to get the values under the header column into a variable for comparison.
import csv

with open('data.csv', 'rb') as f:
    data = csv.reader(f, delimiter=',')
    for row in data:
        # each row of the csv file is a list of strings so you can get one and compare it with what you want