List Manipulation

Roman rgelfand2 at hotmail.com
Tue Jul 4 10:47:08 EDT 2006


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

Iain King wrote:
> Roman wrote:
> > I would appreciate it if somebody could tell me where I went wrong in
> > the following snipet:
> >
> > When I run I get no result
> >
> > cnt = 0
> > p=[]
> > reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
> >                          quotechar="'", delimiter='\t')
> > for line in reader:
> >     if cnt > 6:
> >        break
> >     for col in line:
> >        p[:0].append(str(col))
>
> What are you trying to do here?  p[:0]  returns a new list, of all the
> elements in p up to element 0 (which is of course the empty list),
> which is then appended to, but is not stored anywhere.  If you want to
> insert str(col) then use p.insert
> 
> 
> Iain




More information about the Python-list mailing list