CSV file row and column iteration

820 Views Asked by At

I'm having trouble trying to return row 1 of a csv file, which basically contains a string of data: [0, 300, 30, 521, 53, 462, 642]. Basically I'm trying to delete the first column, and then iterate through the first row [300, 30, 521, 53, 462, 642] and append the minimum value (30) into a list called "b". Then repeat that with the second row and second column and so on. However, I get an error and I'm not sure what it means. Can anyone help me with this? Thank you.

Traceback (most recent call last):
  File "travelsales.py", line 25, in <module>
    nearest(0);
  File "travelsales.py", line 17, in nearest
    for i in reader.next():
StopIteration

Source code:

import time
import csv
from itertools import islice

b = list();
reader = csv.reader(open('distance.csv','rU'), delimiter = ' ');
def delete(column):
    for i in reader:
        del i[column];

def nearest(row):
    a = list();
    list(islice(reader, row));
    for i in reader.next():
        a.append(int(i));
    print a;
    b.append(min(a));
    del a[:];
    print b;

delete(0);
nearest(0);
2

There are 2 best solutions below

3
On

You exhaust your reader in delete - it's similar to reading to the end of a file. This approach is overcomplicated. To meet the revised goal of calculating the min of each row, ignoring the nth column in each nth row, I would do this:

b = []
for rownum, line in enumerate(reader):
    b.append(min(int(x) for x in line[:rownum] + line[rownum + 1:])

Since each line list is already created, I think slicing directly is faster than itertools.islice. List addition vs. itertools.chain I'm not so sure about.

min(min(int(x) for x in line[:rownum]), min(int(x) for x in line[rownum + 1:]))

might also be worth testing.

Also, Python doesn't use semicolons.

0
On
import csv

def read_csv(fname, **kwargs):
    with open(fname, 'rb') as inf:
        in_csv = csv.reader(inf, **kwargs)
        return list(in_csv)

def nearest(row):
    return min(int(i) for i in islice(row, 1, None))

def main():
    data = read_csv('distance.csv', delimiter=' ')
    b = [nearest(row) for row in data]

if __name__=="__main__":
    main()