简体   繁体   中英

Removing escape characters from a string

How can i remove the escape chars in Python 2.7 and python 3 ?

Example:

a = "\u00E7a\u00E7a\u00E7a=http\://\u00E1\u00E9\u00ED\u00F3\u00FA\u00E7/()\=)(){[]}"
decoded =  a.decode('unicode_escape')
print decoded

Result:

çaçaça=http\://áéíóúç/()\=)(){[]}

Expected result

çaçaça=http://áéíóúç/()=)(){[]}

EDIT: In order to avoid unnecessary downvotes. using .replace isn't our primary focus since this problem was raised by a legacy solution from other teams ( db table with reference data with contains portuguese chars and regular expressions).

You're looking for a simple str.replace

>>> print decoded.replace('\\', '')
çaçaça=http://áéíóúç/()=)(){[]}

The remaining \\ is actually a literal backslash, not an escape sequence.

You can simply remove the unnecessary the escape character in your string, ie

>>> a = "\u00E7a\u00E7a\u00E7a=http://\u00E1\u00E9\u00ED\u00F3\u00FA\u00E7/()=)(){[]}"
>>> decoded =  a.decode('unicode_escape')
>>> print decoded
çaçaça=http://áéíóúç/()=)(){[]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM