read xml file from compressed file using gzip

John Machin sjmachin at lexicon.net
Sun Jun 10 06:40:24 EDT 2007


On 10/06/2007 8:08 PM, flebber wrote:
> 
> Thanks that was so helpful to see how to do it. I have read a lot but
> it wasn't sinking in, and sometimes its better to learn by doing.

IMHO it's always better to learn by: read some, try it out, read some, ...

> Some
> of the books I have read just seem to go from theory to theory with
> the occasional example ( which is meant to show us how good the author
> is rather than help us).

Well, that's the wrong sort of book for learning a language. You need 
one with little exercises on each page, plus a couple of bigger ones per 
chapter. It helps to get used to looking things up in the manual. 
Compare the description in the manual with what's in the book.

> 
> For the record
> 
>>>> ## working on region in file /usr/tmp/python-F_C5sr.py...
> ['mimetype', 'maindata.xml']
> File Name
> Modified             Size
> mimetype                                       2007-05-27
> 20:36:20           17
> maindata.xml                                   2007-05-27
> 20:36:20        10795
>>>> print len(xml_string)
> 10795
>>>> for line in xml_string:
>    print line
> ... ...
> <
> ?
> x
> m
> l
> 
> v
> e
> r
> s
> i.....(etc ...it went for a while)

Yup. At a rough guess, I'd say it printed 10795 lines.

So now you've learned by doing it what
     for x in a_string:
does :-)

I hope you've also learned that "xml_string" was a good name and "line" 
wasn't quite so good.

> 
> and
> 
>>>> lines = xml_string.splitlines()

Have you looked up splitlines in the manual?


>>>> print len(lines)
> 387
>>>> print len(lines[0])
> 38
>>>> for line in lines:
> ... print line
>   File "<stdin>", line 2
>     print line
>         ^
> IndentationError: expected an indented block
>>>> for line in lines:
>     print line
> 

After you fixed your indentation error, did it look like what you 
expected to find?

Cheers,
John



More information about the Python-list mailing list