0
votes

My task is to compare two CSV files and find where there are differences between them. Maybe calculate average for each column. Right now, I'm trying to read the CSV file, convert data to float and create a two-dimensional list-based array for future column average calculation. And I'm stuck. I have two CSV files like this, they are the same but with different headers:

Extraction  DATA                
Start = 01-01-01 08:00:00.000  End = 01-01-01 08:01:00.000  Period =  6.00E+0                   

Request     Request     Name1     Name2       Name3      Name4        
Date        Time        Descr1    Desc2       Descr3     Descr3     
yy-mm-dd    hh:mm:ss.sss mm       mm         mm           mm 

2001/01/24  00:00.0 1.00E+03    1.00E+03    -1.00E-04   8.00E-04
2001/01/25  00:01.0 2.00E+03    2.00E+03    -2.00E-04   9.00E-04
2001/01/26  00:02.0 3.00E+03    3.00E+03    -3.00E-04   1.00E-03
2001/01/27  00:03.0 4.00E+03    4.00E+03    -4.00E-04   1.10E-03
2001/01/28  00:04.0 5.00E+03    5.00E+03    -5.00E-04   1.20E-03
2001/01/29  00:05.0 6.00E+03    6.00E+03    -6.00E-04   1.30E-03
2001/01/30  00:06.0 7.00E+03    7.00E+03    -7.00E-04   1.40E-03
2001/01/31  00:07.0 8.00E+03    8.00E+03    -8.00E-04   1.50E-03
2001/02/01  00:08.0 9.00E+03    9.00E+03    -9.00E-04   1.60E-03
2001/02/02  00:09.0 1.00E+04    1.00E+04    -1.00E-03   1.70E-03
2001/02/03  00:10.0 1.10E+04    1.10E+04    -1.10E-03   1.80E-03

I wrote this code:

import csv

skip = 0
my_list = []

#trying to convert to float
def conv(s):
    try:
        s=float(s)
    except ValueError:
        pass    
    return s

with open('Illia1.csv', newline='') as data:    
    reader = csv.reader(data, delimiter=",")
    #Skip 8 rows with headers
    while skip < 8:
        next(reader, None)
        skip += 1
    #Reading rows and writing converted cells into the list
    for row in reader:
        for cell in row:
            y=conv(cell)
            my_list.append(y)
        print(my_list)

And I convert output data into float type, but how I can store the result of the conversion for each row into a separate list. Because right now I have this result:

['2001/01/25', '00:01.0', 2000.0, 2000.0, -0.0002, 0.0009, '2001/01/26', '00:02.0', 3000.0, 3000.0, -0.0003, 0.001, '2001/01/27', '00:03.0', 4000.0, 4000.0, -0.0004, 0.0011, '2001/01/28', '00:04.0', 5000.0, 5000.0, -0.0005, 0.0012, '2001/01/29', '00:05.0', 6000.0, 6000.0, -0.0006, 0.0013, '2001/01/30', '00:06.0', 7000.0, 7000.0, -0.0007, 0.0014, '2001/01/31', '00:07.0', 8000.0, 8000.0, -0.0008, 0.0015, '2001/02/01', '00:08.0', 9000.0, 9000.0, -0.0009, 0.0016, '2001/02/02', '00:09.0', 10000.0, 10000.0, -0.001, 0.0017, '2001/02/03', '00:10.0', 11000.0, 11000.0, -0.0011, 0.0018]

Thanks for your attention. I’m looking forward to your reply.

1

1 Answers

1
votes

You could make my_list a list of lists, where each sub_list corresponds to a row of data. For example,

for row in reader:
    sub_list = []
    for cell in row:
        y = conv(cell)
        sub_list.append(y)
    my_list.append(sub_list)
print(my_list)

This would produce [[row 1 data], [row 2 data],...]. Then you can easily separate the lists by for sub_list in my_list.