开发者

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜