views:

39

answers:

1

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?

+6  A: 
>>> u'\\u4f60\\u4f60'.decode('unicode_escape')
u'\u4f60\u4f60'
KennyTM
+1 You learn something new every day!
katrielalex
Thank you for the quick and useful answer.
kols