简体   繁体   English

从字符串中删除转义字符

[英]Removing escape characters from a string

How can i remove the escape chars in Python 2.7 and python 3 ? 如何在Python 2.7和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). 使用.replace并不是我们的主要重点,因为其他团队的遗留解决方案(带有参考数据的db表包含葡萄牙语字符和正则表达式)引起了这个问题。

You're looking for a simple str.replace 您正在寻找一个简单的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://áéíóúç/()=)(){[]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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