0

I'd like to convert a string in Python to a raw string, doing this programmatically. To say it short, I'd like to add a "r" before my string with a function.

In my case, this is a windows path, like this:

path = "C:\my path\01 with numbers in folder names\02 and\next folder"

The easy way is to write "r" or "R" to read the string as raw, like this:

path = r"C:\my path\01 with numbers in folder names\02 and\next folder"

But I'll get the path from the user, and I want to convert it on the fly to raw before something else happens (in my case I want to convert it to a unix path).

How to do it? Is it really true it is not possible? The only working function I found is this one, but it is quite cumbersome, because it needs a hard coded dictionary.

UPDATE/SOLUTION: This behavior only happens if the string is coming from the python code itself. Reading this string from a gui (e.g. pyside2), it remains as raw string.

print(self.ui.plainTextEdit.toPlainText())
-> C:\my path\01 with numbers in folder names\02 and\next folder
vince
  • 47
  • 1
  • 6
  • https://stackoverflow.com/a/45807360/854434 – smassey May 28 '20 at 19:23
  • 2
    The internal representation of a string and a raw string are identical, the only difference is in the way it's written out in your code. If you get the string from user input, it's a `str` - there's no such thing as a `raw_str` for example. – Mark Ransom May 28 '20 at 19:24
  • between the 'r' before the string stands for [regular expression](https://en.wikipedia.org/wiki/Regular_expression) not raw string – Amin Guermazi May 28 '20 at 19:31
  • 1
    That's not true, it stands for raw string. – ggorlen May 28 '20 at 19:34
  • @AminGuermazi, that is incorrect. [The `r` prefix stands for "raw"](https://docs.python.org/3/reference/lexical_analysis.html?highlight=raw#strings). – ChrisGPT was on strike May 29 '20 at 13:50
  • "UPDATE/SOLUTION: This behavior only happens if the string is coming from the python code itself. Reading this string from a gui (e.g. pyside2), it remains as raw string."—You're definitely misunderstanding. As Mark says, there is no "raw string" data type. Unfortunately, I don't understand your misunderstanding :-). Your "solution" appears identical to the first code snippet which you say you don't want. What operations are you performing on this string that make you think it needs to "be raw"? Because, again, that isn't a thing. – ChrisGPT was on strike May 29 '20 at 14:02
  • @Chris Sorry for the confusion. I wanted to convert a window path. I started writing the functions without the GUI, so I start with the window path as a string in a variable. But the string was interpreted so it was not possible to convert it properly. It just worked if I put "r" before the string. and That was my question: how to put a "r" before a string to not be interpreted. Now I guit the GUI, and if I get the string from the GUI, the string will not be interpreted, so everything works as wanted. I didn't know that, and the behavior with the string was really confusing! – vince May 30 '20 at 09:49
  • Thanks, that makes more sense. Raw strings, prefixed with `r`, only make sense when they're typed literally into your source code. I'm glad you got this working. – ChrisGPT was on strike May 30 '20 at 15:16

0 Answers0