0

Is there a function that has an effect of r'some string'?

For instance we have some variable s = 'a:b\nc', and we want to escape it somewhere later, so what function can do that?

re.escape escapes : too, so it isn't equivalent of r-prefix. Basically r-equivalent function should give a:b\\nc as a raw string, not a\:b\\nc, right?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Nick
  • 453
  • 1
  • 5
  • 7
  • 3
    I don't think `r` does what you think it does. – Mad Physicist Feb 21 '17 at 21:02
  • 4
    Possible duplicate of [Python Raw Strings](http://stackoverflow.com/questions/4415259/python-raw-strings) – Christian König Feb 21 '17 at 21:03
  • 2
    Once a string has already been parsed and is *in memory*, there is no equivalent for `r`. For example, supposed I had, `x = 'an a: \u0041'`, what do you want your function to do to `x`? The value is `x` is equal to the literal `'an a: A'`. If you want `'an a: \\u0041'`, which is what `r` would have done, there's no way to do that since the original literal representation is no longer available to the interpreter. (At least, not without some kind of psycho manual reparsing of a code file or something equally off the wall.) It's not clear what result you want. – jpmc26 Feb 21 '17 at 21:14
  • Thought at first that there is mutually correspondence between values in memories and their representations. So you can go in both directions. – Nick Feb 21 '17 at 21:27
  • 3
    Python's string syntax allows many representations to result in the same string. To extend jpmc26's example, `'A'`, `'\x41'`, `'\u0041'` and `'\U00000041'` all refer to the same character. – Blckknght Feb 21 '17 at 21:52
  • Duplicate: [casting raw strings python](//stackoverflow.com/q/2428117) – Aran-Fey Oct 10 '18 at 17:25

1 Answers1

0

You're probably a bit confused about what the raw prefix on a string literal actually does, but I think I understand the question anyway: you want to escape the backslashes.

This was the closest thing I could find. There is probably a better way:

>>> import codecs
>>> s = 'a:b\nc'
>>> codecs.encode(s, 'unicode-escape').decode('utf-8')
'a:b\\nc'
wim
  • 338,267
  • 99
  • 616
  • 750
  • Thanks, tried `codecs.escape_encode` before and it didn't work. Now I understand that before that I should have turned string into bytes. But that would lead to 3 steps, your approach with `codecs.encode` is two. – Nick Feb 21 '17 at 21:19
  • It's not clear what the OP wants. Consider the string `'an a: \u0041'`. Does the OP want `'an a: A'` or `'an a: \\u0041'`? Your answer returns the former. – jpmc26 Feb 21 '17 at 22:56
  • @jpmc26 You're right, but it's up to them to clarify it if they want any more help... – wim Feb 21 '17 at 23:09
  • This won't work for complicated string which contains weird characters and control characters: https://stackoverflow.com/questions/52744656/how-to-format-convert-string-literal-to-raw-string-literal-in-python3-at-run-tim?noredirect=1#comment92414162_52744656 – weefwefwqg3 Oct 10 '18 at 16:36