List Manipulation
Roman
rgelfand2 at hotmail.com
Tue Jul 4 12:33:23 EDT 2006
I am getting
TypeError: unsubscriptable object
when specifying
for line in reader[:7]:
Steven D'Aprano wrote:
> On Tue, 04 Jul 2006 07:01:55 -0700, 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
>
> What do you mean? Does it print None?
>
> > cnt = 0
> > p=[]
> > reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
> > quotechar="'", delimiter='\t')
> > for line in reader:
> > if cnt > 6:
> > break
>
> That's a very unPythonic way of doing the job. The usual way of doing
> this would be something like this:
>
> for line in reader[:7]:
> # no need for the "if cnt > 6: break" clause now
>
>
> > for col in line:
> > p[:0].append(str(col))
>
> p[:0] creates a new list, which has a string appended to it, and is then
> thrown away. What are you trying to do?
>
> If you are trying to insert the new entry at the beginning of the list,
> you probably want this:
>
> p.insert(0, str(col))
More information about the Python-list
mailing list