I have CSV files that contain numerous values that I want to reference. I wanted to parse them succinctly using eval. Here’s what I tried:
line = fileHandle.readline()
while line != "":
if line != "\n":
parameter = line.split(',')[0]
value = line.split(',')[2].replace("\n", "")
eval("%s = \"%s\"" % (parameter, value))
print(parameter + " = " + eval(parameter)) # a quick test
line = fileHandle.readline()
What I get is:
Traceback (innermost last):
File "<string>", line 73, in ?
File "<string>", line 70, in createJMSProviders
File "<string>", line 49, in createJMSProviderFromFile
File "<string>", line 1
externalProviderURL="tibjmsnaming://..."
^
SyntaxError: invalid syntax
I reads to me like it is not possible to eval("externalProviderURL=\"tibjmsnaming://...\""). What is wrong with this statement?
eval()is for evaluation Python expressions, and assignment (a = 1) is a statement.You’ll want
exec().(FYI, to use
eval()you’d have to doexternalProviderURL=eval("\"tibjmsnaming://...\""), but it looks like your situation is more suited toexec).