eval to dict problems NEWB going crazy !

Fredrik Lundh fredrik at pythonware.com
Thu Jul 6 07:51:08 EDT 2006


"manstey" <manstey at csu.edu.au> wrote:

> That doesn't work. I just get an error:
>
>    x = eval(line.strip('\n'))
>  File "<string>", line 1
>     [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
>
> SyntaxError: unexpected EOF while parsing
>
> any other ideas?

hint 1:

>>> eval("[('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]\n")
[('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
>>> eval("[('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]")
[('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]

hint 2:

>>> eval("")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing
>>> eval("\n")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1

    ^
SyntaxError: unexpected EOF while parsing

hint 3: adding a "print" statement *before* the offending line is often a good way
to figure out why something's not working.  "repr()" is also a useful thing:

    if line[0] != '#': # Get rid of comment lines
        print repr(line) # DEBUG: let's see what we're trying to evaluate
        x = eval(line)
        dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is

</F> 






More information about the Python-list mailing list