List Manipulation
Mike Kent
mrmakent at cox.net
Tue Jul 4 11:03:40 EDT 2006
Roman wrote:
> Thanks for your help
>
> My intention is to create matrix based on parsed csv file. So, I would
> like to have a list of columns (which are also lists).
>
> I have made the following changes and it still doesn't work.
>
>
> cnt = 0
> p=[[], [], [], [], [], [], [], [], [], [], []]
> reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
> quotechar="'", delimiter='\t')
> for line in reader:
> if cnt > 6:
> break
> j = 0
> for col in line:
> p[j].append(col)
> j=j+1
> cnt = cnt + 1
>
> print p
p[j] does not give you a reference to an element inside p. It gives
you a new sublist containing one element from p. You then append a
column to that sublist. Then, since you do nothing more with that
sublist, YOU THROW IT AWAY.
Try doing:
p[j] = p[j].append(col)
However, this will still result in inefficient code. Since every line
you read in via the csv reader is already a list, try this (untested)
instead:
reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
quotechar="'", delimiter='\t')
p = [ line for line in reader[:7] ]
More information about the Python-list
mailing list