python syntax error for valid code

   1913   7   0
User Avatar
Member
2 posts
Joined: June 2016
Offline
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
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
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:
test.replace('\', '/')
However, this will return an error:
test.replace('\\', '/')

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.
User Avatar
Member
9380 posts
Joined: July 2007
Offline
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
test.replace('\\', '/')
is actually executing as
test.replace('\', '/')
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
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
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.
User Avatar
Member
9380 posts
Joined: July 2007
Offline
SWest
However, the expected result did still not show up.
worked for me in Python SOP
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
tamte
SWest
However, the expected result did still not show up.
worked for me in Python SOP

Yes, 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.
User Avatar
Member
9380 posts
Joined: July 2007
Offline
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
User Avatar
Member
2 posts
Joined: June 2016
Offline
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
  • Quick Links