Re: parse a normal textfile

2008-10-01 Thread Lie Ryan
On Wed, 01 Oct 2008 14:09:09 +0200, Tino Wildenhain wrote:

> 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

There is no need to use eval on that, you could just use:

f = open('file.conf')
conf = {}
for line in f:
key, value = line.split('=', 1)
conf[key] = value

a bit more obscure:

conf = dict(line.split('=', 1) for line in open('file.conf'))

--
http://mail.python.org/mailman/listinfo/python-list


Re: parse a normal textfile

2008-10-01 Thread Tino Wildenhain

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



smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list

parse a normal textfile

2008-10-01 Thread devi thapa
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

So when I give 'name' as an input, the output must be 'adf'

so please help me out with this.

regards,
devi.
--
http://mail.python.org/mailman/listinfo/python-list