[Tutor] parsing configuration files

Daniel Ehrenberg littledanehren at yahoo.com
Mon Dec 22 21:24:54 EST 2003


Danny Yoo wrote:
> Side note: it might be nicer to have parsefile()
> take in a file-like
> object, rather than a filename.
> 
OK, I'll change that.
> 
> Looking at the main loop:
> 
> ###
>     for line in stuff2parse:
>         if line:
>             if not line.startswith(' '):
>                 currentSection = line[:-1]
>                 data[currentSection] = []
>             else:
>                 try:
>                     data[currentSection] +=
> line.strip()
>                 except: pass
> ###
> 
> There's a subtle type error going on, and it has to
> do with 'data'.

Hmm. I guess this is one of the disadvantages of
Python's relatively weak typing.

> On other style note: when you're iterating over a
> file, you can just say:
> 
>    for line in some_file:
> 
> Files are iterable in Python, so there's no more
> need to say:
> 
>    for line in some_file.xreadlines()
> 
> as long as you're using a relatively recent version
> of Python.
> 
> 
> I hope this helps!
> 

Oops. I just put that in because I thought it somehow
could have been the problem. It used to be 'for line
in some_file:', and now it's fixed to be that again.

Thanks for your help.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Tutor mailing list