How to eval a file

Björn Lindberg d95-bli at nada.kth.se
Mon Feb 24 00:23:24 CET 2003


Alex Martelli <aleax at aleax.it> writes:

> file contains:
> 
> log     /error.txt
> splash  /welcome.png
> icon    ./prism.ico
> login   C:/prism/user.dbm
> cabinet C:/prism/patient.dbm
> 
> and you parse it with:
> 
>     lines = [ line.split() for line in open(filename) ]
>     self.__dict__.update(dict(lines))
> 
> or something like that?

I think this is a better solution as well. If you use the shlex module
to parse it you will get handling of quoting for free. The shlex
module will separate your file into tokens for you, and quoted text is
considered one token. very convenient.


Björn




More information about the Python-list mailing list