0
>>> s = '\\xca'
>>> s
'\\xca'
>>> s.replace('\\x', '\x')
  File "<stdin>", line 1
    s.replace('\\x', '\x')
                         ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

how to bypass the error? to print 'Ê' character instead of \\xca

PS: s.replace('\\xca', '\xca') is not what I want

Kanony
  • 509
  • 2
  • 12
  • 1
    Are you aware `len("\\xca") == 4`? Or... do you mean to code `"\u00ca"`? – Fiddling Bits Aug 11 '22 at 15:45
  • 2
    `s` is not what you think it is. If you want s to be exactly "\\xca" then you have to do `s = r'\\xca'` but then it will be represented as '\\\\xca' when printing it. Could you clarify what are you trying to do? –  Aug 11 '22 at 15:47
  • 1
    Sounds like `s.replace('\\xca', '\xca')` *is* what you want. – khelwood Aug 11 '22 at 15:47
  • Where did that string come from in the first place and how complicated can these strings get? If you just typed it in, then `'\xca'` is what you want. Can you change the source or do you really need to parse this string. – tdelaney Aug 11 '22 at 15:52
  • You can't do this as a replacement because `'\x'` is not an actual character that exists; it's just a partial/incomplete escape sequence that could be extended into a longer description of a character, but it's only used in the way a Python string is represented as text to humans; it's not actually part of that string in memory. – Charles Duffy Aug 11 '22 at 15:52
  • @tdelaney can't I just somehow retrieve it without first backslash? – Kanony Aug 11 '22 at 15:55
  • @Kanony - I don't know what your question is. I was curious whether this is something you've typed in as python literal string - then the solution is just to type it correctly in the first place. If you are getting it programatically, then you can fix it with code. But even then, it may be a question of what generated this string in the first place. – tdelaney Aug 11 '22 at 15:57
  • @tdelaney yes it was generated programatically – Kanony Aug 11 '22 at 15:58
  • 1
    [Does this answer your question?](https://stackoverflow.com/questions/4020539/process-escape-sequences-in-a-string-in-python) – Lecdi Aug 11 '22 at 16:01
  • @Lecdi yes it does – Kanony Aug 11 '22 at 16:03

3 Answers3

3

This looks like a fragment of a string literal. You could have python parse it, but you would also have to add surrounding quotes so that python views it as a string.

import ast
s = '\\xca'
fixed = ast.literal_eval('"' + s + '"')
print(fixed)

Output

Ê
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • I've just discarded my answer proposing to get the `ca` part from the string, convert it to decimal and finally replace it with `chr(...)`, this is simple and charming, nice! – rayt Aug 11 '22 at 16:00
  • Note: this won't work for strings that contain `"`. See the duplicate question for a correct answer. Also see the last edit of [this answer](https://stackoverflow.com/a/70355049) for a fully working solution using a similar approach to this. – Lecdi Aug 11 '22 at 16:03
  • @Lecdi - that's making a lot of assumptions about what the input is. I've asked OP for clarification several times, to no avail. Suppose this comes from a text file where python literal strings, minus the quotes, have been entered. Then this is the correct answer. Suppose its just simple 1 character encodings like presented in the quesiton. Once again, this is the right answer. – tdelaney Aug 11 '22 at 16:23
  • @tdelaney Yes, but without being sure, both answers I mentioned are always correct (will always work), so it is better to use those just to be safe. – Lecdi Aug 11 '22 at 17:49
-1

Simply change s='\\cxa' to s='\cxa' and it will print your desired character. This is because in the former string the first backslash escapes the second one and the last three characters are treated as normal text. While in the latter the three last characters are treated together.

To understand this in more detail you should look into "escape characters".

Filip
  • 41
  • 1
  • 6
-3

What you need to do is escape your '\' characters in your replace so it's actually reading your \ as a \ instead of as an escape for the next character.

Example:

s = '\\xca'
s.replace('\\\\x', '\\x')
print(s)