parse a normal textfile

Tino Wildenhain tino at wildenhain.de
Wed Oct 1 08:09:09 EDT 2008


devi thapa wrote:
> hi all
>  
>            I have one normal text file. I need to parse the file, that 
> too in an associative way .
> suppose that below is the normal textfile
> 
> name='adf'
> id  =1
> value=344
> 

there are many approaches to config files. But
in your special example, it looks like a simplified
mapping, so

parsed=eval("dict(%s)" % ",".join(line
                              for line
                              in file("textfile")
                              if line.strip()
                                  )
             )

 >>> parsed['name']
'adf'

but of course eval() is dangerous, so feel free to
explore more then this one solution.

Regards
Tino

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20081001/5384fed3/attachment.bin>


More information about the Python-list mailing list