开发者

How to convert unicode string like u'\\u4f60\\u4f60' to u'\u4f60\u4f60' in Python?

开发者 https://www.devze.com 2023-01-15 05:39 出处:网络
I capture the string from a html source file using regex: f = open(rrfile, \'r\') p = re.compile(r\'\"name\":\"([^\"]+)\",\"head\":\"([^\"]+)\"\')

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

精彩评论

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