Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python raw string assignment

Given a file contains lines such as:

(?i:\bsys\.user_catalog\b)

While reading those line, I want the value to be a raw string (unescaped), meaning, in memory, line should be

r'(?i:\bsys\.user_catalog\b)'

instead of

(?i:\bsys\.user_catalog\b)

Which is escaped when passed over to libs such as sqlobject.

For instance, with sqlobject, if I state

Table(column=r'(?i:\bsys\.user_catalog\b)')

I get the desired results while if I state

Table(column='(?i:\bsys\.user_catalog\b)')

I do not.

So question is basically, I can I pass a raw string when I am not in declarative/assignment mode (e.g. a = r'string'), rather, the string is already in memory.

like image 966
Tzury Bar Yochay Avatar asked Nov 24 '11 08:11

Tzury Bar Yochay


People also ask

How do you assign a raw string in Python?

Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.

What is a raw string?

A raw string in programming allows all characters in a string literal to remain the same in code and in the material, rather than performing their standard programming functions. Raw strings are denoted with the letter r, or capital R, and might look something like this: R “(hello)”

What does \r do in string?

r means the string will be treated as raw string. See the official Python 2 Reference about "String literals": When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.

What does \r do in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.


2 Answers

The raw string notation is only used in Python source code; all strings declared as raw strings are "converted" to normal strings with the necessary escape sequences added during "compile time" (unlike (in Python 2) the two different string types string/Unicode string):

>>> r"\b"
'\\b'
>>> "Hello"
'Hello' 
>>> u"Hello"
u'Hello'

If you read the string from a file, it will already be correctly escaped.

(Assuming test.txt contains (?i:\bsys\.user_catalog\b)):

f = open("test.txt").read()
print f
print repr(f)

Output:

(?i:\bsys\.user_catalog\b)
'(?i:\\bsys\\.user_catalog\\b)'
like image 188
Tim Pietzcker Avatar answered Oct 19 '22 16:10

Tim Pietzcker


You can use raw string anywhere you are using a string. Raw string is just a user friendly way to represent a string when you have lots of escape characters.

The second case is not working because of the '\'. So you need to escape it using another '\'. The second case should work if you give '(?i:\\bsys\\.user_catalog\\b)'. In memory, since ASCII or Unicode is stored, it doesn't make any difference if it is raw string or not.

like image 2
M S Avatar answered Oct 19 '22 14:10

M S