简体   繁体   中英

Python: difficult in converting csv to list of list

I want to read a .csv file which has data format like

-179.750  71.250   -26.7   -19.5   -22.5   -22.3    -8.0    -0.6     2.5 
-179.750  68.750   -28.5   -21.3   -24.4   -24.4    -8.0     0.0     4.0
.....

and I want to convert to list of list as

[[-179.750,71.250..2.5],[-179.750,68.750,..4.0]

I use csv module to read csv file as:

 import csv
 csvfile= open('test.csv','rU')
 reader = csv.reader(csvfile,quotechar=" ")
 allRows = list(reader)
 print allRows

The output is

[['-179.750  68.750 ... -26.5'],['-179.750  68.250 ... 4.0']]

please give me some idea so that i can modify and get my output. Thanks.

The quotechar argument is for the character to be used to enclose a data entry containing delimiters, etc. You should use delimiter .

If you want numbers out, not lists of strings, you'll want to also put the results through float .

import csv
csvfile= open('test.csv','rU')
reader = csv.reader(csvfile,delimiter=" ")
allRows = list(reader)
print allRows

numData=[ [float(i) for i in row]
          for row in allRows]

print numData

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM