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 ?

6
  • 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] Commented Nov 11, 2022 at 1: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. Commented Nov 11, 2022 at 1:57
  • Thank you so much Karl. Could you please explain how can overcome it please, Commented Nov 11, 2022 at 1:58
  • Please see the linked duplicate. Also consider where your data is coming from and why it looks the way it does. Commented Nov 11, 2022 at 2: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] Commented Nov 11, 2022 at 2:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.