python syntax error for valid code
1913
7
0
March 5, 2023 5:19 p.m.
Hello,
I get a syntax error with the following lines of code in a python sop:
test = 'my\string'
test . replace ( ' \\ ' , '/' )
Python error: File "<stdin>", line 2
test.replace('\', '/')
^
SyntaxError: EOL while scanning string literal
I'm pretty sure the code is correct and it works when executing it in my Linux terminal. However, before logging a bug I wanted to check if I'm missing something obvious, or if it's just me the code isn't working for.
Thank you,
Stefan
SWest
Member
313 posts
Joined: Oct. 2016
Offline
March 7, 2023 2:36 p.m.
actinidiaFX test = 'my\string' test.replace('\\', '/')Python 3.9.10 ( main , Sep 15 2022 , 18 : 00 : 44 )
[ GCC 9.3.1 20200408 ( Red Hat 9.3.1 - 2 )] on linux
Houdini 19.5.403 hou module imported .
Type "help" , "copyright" , "credits" or "license" for more information .
>>> test = 'my\string'
>>> test . replace ( ' \\ ' , '/' )
'my/string'
>>>
Pasted your example as is and there is no error with it in the Houdini Python shell over here.
Howbeit, it will give an error in the Python SOP as you stated. Not sure why.
This will run:
However, this will return an error:
I'd say it looks like a bug, behaves like a bug, so it probably is a bug.
Edited by SWest - March 7, 2023 14:56:32
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
tamte
Member
9380 posts
Joined: July 2007
Offline
March 7, 2023 3:14 p.m.
I don't think it's a bug
the code simply gets expanded first before executed as Python to evaluate potential inserted variables or expressions into the code
so what you see as
is actually executing as
which is the main problem
so you may need to do:
test = 'my\string'
test . replace ( ' \\\\ ' , '/' )
Edited by tamte - March 7, 2023 15:14:55
Tomas Slancik CG Supervisor Framestore, NY
SWest
Member
313 posts
Joined: Oct. 2016
Offline
March 7, 2023 3:21 p.m.
Thanks Tamte for your explanation. However, the expected result did still not show up.
Otherwise, try this
import re
a = 'my\string'
a = re . sub ( r ' \\\\ ' , '/' , a )
hou . ui . displayMessage ( a )
I guess it is a bit late over here and I should not do problem solving at this time.
However, this will also work.
test = 'my\string'
test = test . replace ( ' \\\\ ' , '/' )
hou . ui . displayMessage ( test )
So there is no bug in this case.
Edited by SWest - March 7, 2023 15:53:29
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
tamte
Member
9380 posts
Joined: July 2007
Offline
March 7, 2023 3:51 p.m.
SWest However, the expected result did still not show up.worked for me in Python SOP
Tomas Slancik CG Supervisor Framestore, NY
SWest
Member
313 posts
Joined: Oct. 2016
Offline
March 7, 2023 3:56 p.m.
tamte SWest However, the expected result did still not show up. worked for me in Python SOPYes, I just copy pasted the code but there was an assignment operator missing.
From this:
test = 'my\string'
test . replace ( ' \\\\ ' , '/' )
Obviously, nothing will change really, unless the result is assigned.
Edited by SWest - March 7, 2023 15:57:55
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
tamte
Member
9380 posts
Joined: July 2007
Offline
March 7, 2023 4 p.m.
right, the code was just fixed OP snippet, which is not complete, but wrap it in print or assign to variable and the result seems correct
Edited by tamte - March 7, 2023 16:01:20
Tomas Slancik CG Supervisor Framestore, NY
March 12, 2023 8:22 p.m.
Hi tamte and SWest, Thank you both very much for your help! The 4 backslashes indeed do the trick and printing the result returns what you would expect. I actually had tried it with 3 backslashes but considering tamte's explanation it makes sense why it has to be 4. Cheers, Stefan