Reputation: 357
I'm trying to use eval function to execute a loop. It gives a syntax error
list_subjects = (element.upper() for element in list(score_card_data['subject_id']))
for i,sub in enumerate(list_subjects) :
print(("bins_{1:s}").format(i,sub))
print("list(score_card_data.loc[score_card_data['subject_id'] == {1:s}, 'bin_list'])").format(i,sub)
eval("("bins_{1:s}").format(i,sub) = "list(score_card_data.loc[score_card_data['subject_id'] == {1:s}, 'bin_list'])").format(i,sub)")
File "<ipython-input-192-529c79a094e4>", line 5
eval("("bins_{1:s}").format(i,sub) = "list(score_card_data.loc[score_card_data['subject_id'] == {1:s}, 'bin_list'])").format(i,sub)")
^
SyntaxError: invalid syntax
How do I resolve the 2 print statements in one eval function
Upvotes: 1
Views: 750
Reputation: 6746
You get a syntax error because you try to use the same type of quotes inside a string that is used to delimit the string literal in your code.
You have those options:
Use single quotes inside the string and double quotes to delimit it:
eval("' '.join('some', 'words')")
Use double quotes inside the string and single quotes to delimit it:
eval('" ".join("some", "words")')
Use any quotes inside the string and any quotes (the same type on the left and right side of course) to delimit it, but escape all quotation marks inside the string using a backslash:
eval('\' \'.join(\'some\', \'words\')')
eval("\" \".join(\"some\", \"words\")")
Use any quotes inside the string and "triple quotes" (either three single quotes '''
or three double quotes """
, same type on the left and right side of course) to delimit it:
eval("""" ".join("some", "words")""")
eval("""' '.join('some', 'words')""")
eval('''' '.join('some', 'words')''')
eval('''" ".join("some", "words")''')
Upvotes: 2