How to eval a file

Alex Martelli aleax at aleax.it
Mon Feb 24 03:22:49 EST 2003


Donnal Walter wrote:
   ...
>> 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?
> 
> Ah, yes. I like that much better. I may also let Björn talk me into
> trying out the shlex module as well. Thanks.

shlex is good if you need it, but for this specific use you
can probably do without it.  The keys must be identifiers,
given the use you make of them, so you do not need to cater
for their being quoted.  If you do need to allow spaces in
the filenames, then the quoting that shlex affords may indeed
be the soundest way, though (because line.split(None, 1) would
not remove the trailing \n the way line.split() does...].


Alex





More information about the Python-list mailing list