user2098583
user2098583

Reputation: 3

Read .csv file with nested for loops, only getting last line

I am trying to read in .csv file and create a 2-D array of floats values. This is what I have done so far:

import csv
filename  = 'data_out.csv'   
# create an array for the for the data    
row_values = [None]*len(header)   
all_values = []

reader = csv.reader(open(filename,'rt'),delimiter=',')
next(reader) #skip header line
for row in reader:
    i=0
    for item in row:
        value = float(item)
        row_values[i]=value
        i=i+1
        print(row_values)
    all_values.append(row_values)

print(all_values)

The print (row_values) looks like it works. Each row is put into a list of float values. But when I print (all_values), it does not work. I have created a list of lists (the correct number of rows), but it is only the last row of values repeated over and over again. Perhaps I have the .append() in the wrong spot?

I should say this is my first time programming in python, but I have been programming in C++ for years. Any help/tips would much appreciated. Thank you!

Upvotes: 0

Views: 1683

Answers (2)

rainer
rainer

Reputation: 7099

The problem is that all_values is storing a reference to the array row_values every time you append it, not a copy. Therefore, all elements of all_values are references pointing to the same array, the values of which you are overwriting the values in row_values in each iteration.

See this, for example:

b = []

a = [1, 2]
b.append(a)
print b

a[0] = 3
b.append(a)
print b

print id(b[0]), id(b[1])

This has the same effect as your program, it will print [[3, 2], [3, 2]] for the second print statement. The last print statement shows the id of both values in b, which is the same, meaning that the two elements in b are the same element.

Therefore, a fixed (and more pythonic) version of your loop would be:

reader = csv.reader(open(filename,'rt'),delimiter=',')
for row in reader:
    row_values = [float(item) for item in row]
    all_values.append(row_values)

print all_values

The third line uses a list comprehension to convert all values in row to floats and store them in a new array, which is then appended to all_values.

Upvotes: 1

longhua
longhua

Reputation: 4252

You only create one array object and append the same array object to the list. So only last row repeats in final output.

You should create the array object inside first level loop and append it to the list.

Upvotes: 1

Related Questions