python - Import csv as List of List of Integers -
i have following csv file
12,29,63,44 54,43,65,34
i'm trying import list of list such each index integer. here's have far.
import csv filename = 'file.csv' open(filename, 'ru') p: #reads csv list of lists my_list = list(list(rec) rec in csv.reader(p, delimiter=',')) print my_list >>> [['12','29','63','44'],['54','43','65','34']]
as can see produces list of list of strings, not integers. how import csv file list of list of integers? this
>>> [[12,29,63,44],[54,43,65,34]]
map int:
my_list = [list(map(int,rec)) rec in csv.reader(p, delimiter=',')] [[12, 29, 63, 44], [54, 43, 65, 34]]
which equivalent to:
my_list = [[int(x) x in rec] rec in csv.reader(p, delimiter=',')]
Comments
Post a Comment