正常情况下
>>> myString = "spam\\neggs"
>>> print(myString)
spam\neggs
期望
>>> print(process(myString))
spam
eggs
通用解决方案
import re
import codecs
ESCAPE_SEQUENCE_RE = re.compile(r'''
( \\U........ # 8-digit hex escapes
| \\u.... # 4-digit hex escapes
| \\x.. # 2-digit hex escapes
| \\[0-7]{1,3} # Octal escapes
| \\N\{[^}]+\} # Unicode characters by name
| \\[\\'"abfnrtv] # Single-character escapes
)''', re.UNICODE | re.VERBOSE)
def decode_escapes(s):
def decode_match(match):
return codecs.decode(match.group(0), 'unicode-escape')
return ESCAPE_SEQUENCE_RE.sub(decode_match, s)
结果
>>> print(decode_escapes('Ernő \\t Rubik'))
Ernő Rubik
Process escape sequences in a string in Python[stackoverflow]
但是仍然处理不了被转义的json字符串
服务器返回的json字符串为已经转义的json字符串像这样:
{"data1":";base64,\/9j\/4AAQSkZJRgABAQEAYABgAAD\/\/gA+Q1J....
在经过python请求后获取的响应, 在内存中看到的是这样:
也就是程序又继续转义了一层
解决方案
def decode_special_escapes(s):
return re.sub(r'\\/', r'/', s)
结果