eval to dict problems NEWB going crazy !

Bruno Desthuilliers onurb at xiludom.gro
Thu Jul 6 07:04:54 EDT 2006


manstey wrote:
> Hi,
> 
> I have a text file called a.txt:
> 
> # comments
> [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
> [('recId', 5), ('parse', {'pos': u'np', 'gen': u'm'})]
> [('recId', 7 ), ('parse', {'pos': u'np', 'gen': u'm'})]
> 
> I read it using this:
> 
> filAnsMorph = codecs.open('a.txt', 'r', 'utf-8') # Initialise input
> file
> dicAnsMorph = {}
> for line in filAnsMorph:
>     if line[0] != '#': # Get rid of comment lines
>         x = eval(line)
>         dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is
> value
> 
> But it crashes every time on x = eval(line). Why is this? If I change
> a.txt to:
> 
> # comments
> [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
> 
> it works fine. Why doesn't it work with multiple lines? it's driving me
> crazy!

try with:
  x = eval(line.strip('\n'))


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list