Python Forum
Open previous csv file to read value under column for comparison - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Open previous csv file to read value under column for comparison (/thread-1571.html)



Open previous csv file to read value under column for comparison - DBS - Jan-13-2017

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)



RE: Open previous csv file to read value under column for comparison - wavic - Jan-13-2017

Hello!
Just keep the values in a variable and when read the second .csv compare them


RE: Open previous csv file to read value under column for comparison - DBS - Jan-13-2017

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.


RE: Open previous csv file to read value under column for comparison - wavic - Jan-13-2017

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