user1681664 user1681664 - 5 months ago 8
Python Question

csv row import into python array

I have csv file in the following format

a b c d
1 12.0 3.5 4.3 5.9

2 13.0 5.7 2.8 5.2

3 14.0 6.4 9.7 2.3

4 15.0 6.8 4.7 3.4


I want to export rows into a python array of arrays. Here is the pseudocode:

a = read csv
b[][] = a float 2d array that is 1x4
import rows into b

the output of b should be:
[[12.0,3.5,4.3,5.9],[13.0,5.7,2.8,5.2],[14.0,6.4,9.7,2.3],[15.0,6.8,4.7,3.4]]


how would I do this? Please let me know if you need any other clarification. Thank you.

Problems:
all rows are NOT of same size. some rows have 10 elements and others may have 7 or 8 or 9.

This is what I have:

import csv
def main():
a = range(4)
x = 0
with open('test.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
a[x] = row
x += 1
print a


Output:

[['13,4.2,2.4,5,6.4'], ['14,3.2,3.4,5.6,7.2'], ['15,8.5,3.7,8.5,0.75'], ['16,5.4,8.3,3.5,5.4']]


How do I make the arrays turn from string into floats?

Answer

Using module csv.DictReader to skip empty lines and get a list of dictionaries:

In [131]: import csv
     ...: with open('a.csv') as f:
     ...:     lst=list(csv.DictReader(f))

In [132]: lst
Out[132]: 
[{'a': '12.0', 'b': '3.5', 'c': '4.3', 'd': '5.9'},
 {'a': '13.0', 'b': '5.7', 'c': '2.8', 'd': '5.2'},
 {'a': '14.0', 'b': '6.4', 'c': '9.7', 'd': '2.3'},
 {'a': '15.0', 'b': '6.8', 'c': '4.7', 'd': '3.4'}]

In [134]: [{k:float(d[k]) for k in d} for d in lst] #convert values to floats
Out[134]: 
[{'a': 12.0, 'b': 3.5, 'c': 4.3, 'd': 5.9},
 {'a': 13.0, 'b': 5.7, 'c': 2.8, 'd': 5.2},
 {'a': 14.0, 'b': 6.4, 'c': 9.7, 'd': 2.3},
 {'a': 15.0, 'b': 6.8, 'c': 4.7, 'd': 3.4}]

EDIT:

to get a list of list:

In [143]: with open('a.csv') as f:
     ...:     cr=csv.reader(f)
     ...:     skip=next(cr)  #skip the first row of keys "a,b,c,d"
     ...:     print [map(float, l) for l in cr]
     ...: 
[[12.0, 3.5, 4.3, 5.9], [13.0, 5.7, 2.8, 5.2], [14.0, 6.4, 9.7, 2.3], [15.0, 6.8, 4.7, 3.4]]
Comments