How to convert unicode string like u'\\u4f60\\u4f60' to u'\u4f60\u4f60' in Python?
I capture the string from a html source file using regex:
f = open(rrfile, 'r')
p = re.compile(r'"name":"([^"]+)","head":"([^"]+)"')
match = re.findall(p, f.read())
And I've tried:
>>> u'\\u4f60\\u4f60'.replace('\\u', '\u')
u'\\u4f60\\u4f60'
>>> u'\\u4f60\\u4f60'.replace(u'\\u', '\u')
u'\\u4f60\\u4f60'
>>> u'\\u4f60\\u4f60'.replace('\\u', u'\u')
File "<stdin>", line 开发者_开发知识库1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence
Could that be done by str.replace()? Or need something more complex?
>>> u'\\u4f60\\u4f60'.decode('unicode_escape')
u'\u4f60\u4f60'
精彩评论