List Manipulation

Mike Kent mrmakent at cox.net
Tue Jul 4 10:17:23 EDT 2006


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))
>     cnt = cnt + 1
>
> print p

I'm having trouble deciding what you *intend* this program to do.  It
looks like you want to take the first 7 lines of the input file, and
append all the data elements in those lines into one long list.  If
that's what you want to do, then you are almost there, although you
could have written it better.  If that's NOT what you want to do...
well, there are tutorials.

The problem with this code is in the line 'p[:0].append(str(col)).
Given a list p, p[:0] will give you the part of p *prior* to element 0.
 Since there is never anything in a list prior to element 0, you will
always get an empty list back.

I assume this is not what you intended.  But just *what* do you intend?
 I sure can't say.




More information about the Python-list mailing list