List Manipulation

Sibylle Koczian Sibylle.Koczian at Bibliothek.Uni-Augsburg.de
Tue Jul 4 10:52:38 EDT 2006


Roman schrieb:
> 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))

This is wrong. I'm not absolutely certain _what_ it does, but it doesn't
append anything to list p. p[:0] is an empty copy of p, you are
appending to this empty copy, not to p. What's wrong with
p.append(str(col))?

> when I change it to the following, I get rows back
> 
> 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:
>        print col
>     cnt = cnt + 1
> 
> print p
> 

Here you print every single cell, but p doesn't change.

HTH
Koczian

-- 
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Sibylle.Koczian at Bibliothek.Uni-Augsburg.DE



More information about the Python-list mailing list