7

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.

Tzury Bar Yochay
  • 8,798
  • 5
  • 49
  • 73

3 Answers3

10

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)'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 6
    "If you read the string from a file, it will already be correctly escaped." This is somewhat misleading; more accurately, the concept of "escaping" doesn't apply to a string **object** but only to a string **literal**. If you read one backslash from a file, you get a string with one backslash in it. If you write `"\\"`, you get a string with one backslash in it. The escaping only happens in the source code so that there is a clear way to say what's part of the string and what's part of the rest of the code. Once the string object is created, that's already clear. – Karl Knechtel Nov 24 '11 at 11:10
  • Similarly, raw strings aren't "converted"; they're an *alternate syntax* for creating *the same type of object*. – Karl Knechtel Nov 24 '11 at 11:11
  • @KarlKnechtel: Thanks for the clarification. I wish I could have written it like this :) – Tim Pietzcker Nov 24 '11 at 11:35
  • Python2 is still python –  Oct 24 '18 at 19:22
2

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.

Chen Levy
  • 15,438
  • 17
  • 74
  • 92
M S
  • 3,995
  • 3
  • 25
  • 36
1

You are thinking about this the wrong way. There isn't a "raw string" type, only "string". Writing r in front of the quotes only tells python how to interpret the following string.

http://docs.python.org/reference/lexical_analysis.html#string-literals

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79