Remove escape character from string

I would like to rotate this line:

a = '\\a' 

in that

 b = '\a' 

There seems to be no obvious way to do this with replace ?

EDIT: more precisely, I want to change the backslash escaping to avoid the a character

+5
source share
3 answers

The character '\ a' is the ASCII BEL character, chr (7).

Performing a conversion in Python 2:

 from __future__ import print_function a = '\\a' c = a.decode('string-escape') print(repr(a), repr(c)) 

Output

 '\\a' '\x07' 

And for future use in Python 3:

 a = '\\a' b = bytes(a, encoding='ascii') c = b.decode('unicode-escape') print(repr(a), repr(c)) 

This gives identical output to the above snippet.

In Python 3, if you were working with byte objects, you would do something like this:

 a = b'\\a' c = bytes(a.decode('unicode-escape'), 'ascii') print(repr(a), repr(c)) 

Output

 b'\\a' b'\x07' 

As Antti Haapala mentions, this simple strategy for Python 3 will not work if the original string contains Unicode characters. In this case, please see His answer for a more reliable solution.

+5
source

In Python 2 you can use

 >>> '\\a'.decode('string_escape') '\x07' 

Note that \a displayed as \x07 .

If the string is a unicode string with extended characters, you must first decode it to a byte string, otherwise the default encoding (ascii!) Is used to convert the unicode object to a byte string first.


However, this codec does not exist in Python 3, and everything is much more complicated. You can use unicode-escape to decode, but it is very broken if the source string contains Unicode characters:

 >>> '\aäầ'.encode().decode('unicode_escape') '\x07äầ' 

The resulting string does not consist of Unicode characters, but bytes decoded as Latin-1. The solution is to re-encode Latin-1, and then decode again as utf8:

 >>> '\\aäầ\u1234'.encode().decode('unicode_escape').encode('latin1').decode() '\x07äầሴ' 
+5
source

Unescape string is what I was looking to find this:

 >>> a = r'\a' >>> a.encode().decode('unicode-escape') '\x07' >>> '\a' '\x07' 

This is the way to do it with unicode. Since you are in Python 2 and cannot use unicode, you can actually have one:

 >>> a.decode('string-escape') '\x07' 
+1
source

Source: https://habr.com/ru/post/1259377/


All Articles