0

i am getting syntax error while executing words[list(eval(s).keys())[0]] = list(eval(s).values())[0]; where words is an empty dictionary and s is my dictionary with string keys and values in a text file.

Can some one please help with the error ?

i am getting syntax error while executing words[list(eval(s).keys())[0]] = list(eval(s).values())[0]; where words is an empty dictionary and s is my dictionary with string keys and values in a text file.

Below is the error:

 words[list(eval(s).keys())[0]] = list(eval(s).values())[0]
  File "<string>", line 1
    s= {'water':{'german':'wasser','french':'leau','spanish':'agua'}, 
     ^
SyntaxError: invalid syntax

Can some one please help with the error ?

  • This is my code : with open("vocabulary.txt", mode = 'r') as f: words ={} while s:=f.readline(): words[list(eval(s).keys())[0]] = list(eval(s).values())[0] – Bodugu Janardana Rao Nov 11 '22 at 01:55
  • Exactly as the error message indicates: `eval` is for **expressions**, not statements (such as an assignment). It cannot accept the `s=` part. That said: [**Do not ever use `eval` (or `exec`) on data that could possibly come from outside the program in any form. It is a critical security risk. You allow the author of the data to run arbitrary code on your computer.**](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – Karl Knechtel Nov 11 '22 at 01:57
  • Thank you so much Karl. Could you please explain how can overcome it please, – Bodugu Janardana Rao Nov 11 '22 at 01:58
  • Please see the linked duplicate. Also consider where your data is coming from and why it looks the way it does. – Karl Knechtel Nov 11 '22 at 02:01
  • Since i am a beginner in Python, I couldn't completely understand the way forward, Karl. below is my code expression and i am stuck to proceed further. Please help. with open("vocabulary.txt", mode = 'r') as f: words ={} while s:=f.readline(): words[list(eval(s).keys())[0]] = list(eval(s).values())[0] – Bodugu Janardana Rao Nov 11 '22 at 02:05
  • This isn't a discussion forum and we don't solve individual users' code issues here; we answer specific questions as they are asked. If you require personalized help, please try another site such as Reddit. Also, please do not try to show multi-line code in the comments. That said, no matter who you ask, much more context would be required to understand the problem properly. – Karl Knechtel Nov 11 '22 at 02:07

0 Answers0