Title: Python program error shows syntax error when there are no error in the syntax
Tags: python;error-handling;syntax-error
Question: I have a code which reads from a formula file as well as from a value file to print the evaluated value into another output descriptor file. The formula file looks like this:
a+b*c m=0.56
(a/b)*(c/d) m=0.44
(c-a)*a m=0.23
<50000 entries&gt;
Input value file looks like:
name    a    b    c    d
pele   0.1  23   -0.4  1.6
dida   1    11.2  0.9  7.2
kaka   2.3  10.9 -0.34 13.2
<100 entries&gt;
and the output descriptor file looks like
100.32 34.11 0.33 -9.54 ...
<100 x 50000&gt; shaped file
Now while the code reads from the formula file it strips from any unwanted addition other than the formula (like the m=0.23 etc) and iterates over every row of the data file to generate a single value which it writes in the output descriptor file. So, in short, a row in the output descriptor file are the values of all the formulae for each specimen (e.g. pele). This it does for all specimens.
the minimal working example looks something like this
for i,r in data.iterrows():
# <reads value from data/value file to variables&gt;
for j in range(len(df)):
f = str(df.iloc[j])
p=f[1:].strip() #to remove a 0 and all leading spaces
sep = '\n'
rest = p.split(sep, 1)[0] #to remove dtype=object and other such info
e = eval(rest)
print(e, end=" ")
print("\n")
This runs fine till the 501th formula of the first specimen, after which it throws an error message like:
Traceback (most recent call last):
File "part1.py", line 46, in <module&gt;
e = eval(rest)
File "<string&gt;", line 1
(((a+c)+abs(a-b))/((d*c)*abs(a-b...
^
SyntaxError: invalid syntax
I checked the formula file at that position and it lists the entire formula, and not this ... as shown in the error. Now I am totally foxed about this and have no clue where things are going wrong. Any help is appreciated.
Comment: `(((eaA+kslB)+abs(eaA-eaB))/((ZA*rsB)*abs(rpB-rsB)))  corr=      0.9521`

Comment: `(((eaA+kslB)+abs(eaA-eaB))/((ZA*rsB)*abs(rpB-r...`

Comment: Thats the actual formula (from the formula file) and the subsequent error. and it defaults at 504th line/formula to be precise.

Comment: @ChrisDoyle if i print the rest, it shows the same one which crops up in the error

Comment: @Chris Doyle Yes, that is what confuses me too. Looked into it for more than an hour, couldn't find any possible reasons.

Comment: I tried removing the particular formula, and doing it again, it gets stuck on another formula somewhere around 716 with the same error

Comment: @furas i did remove them before evaluating.

Comment: I just reviewed the expressions which were getting flagged. Apparently, it does so whenever the character length exceeds 77. Does this help anyhow?

Comment: How does the formula looks like in the file?

Comment: can you show the formula from line 501

Comment: or better yet before each eval of rest, just print rest

Comment: yeah but it should show the full value rather then the .....

Comment: Is the 501st line indented?

Comment: all your formulas are correct for human, but not for Python. Python can calculate `a+b*c` (if you definde first `a`, `b`, `c`) or `m=0.56` but both in one line is not correct Python code. Your formulas has to be correct Python code.

Comment: I would first uses `print(rest)` before `eval()` to see if you correctly remove other elements. It you keep `corr= 0.9521` in your example then it gives `invalid syntax`.  If formulas are indenpendent then I would use `try/except` to catch error, display formula which makes problem and check other formulas.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 15, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:12/30/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><error-handling><syntax-error>', 'Title': 'Python program error shows syntax error when there are no error in the syntax', 'ViewCount': 84, 'id': 59530185, 'user_AboutMe': '<p>I look at words and numbers and try and make out sense out of it. Patterns, data, learnings by machine, both deep and shallow are my interests. However, I am trained to be a material scientist. adds to the flair. :)</p>\n', 'user_AccountId': 6389051.0, 'user_DisplayName': 'mitra', 'user_Reputation': 29.0, 'user_Views': 7.0, 'user_id': 8182398.0}}