I’m scraping info from Facebook which compiles weirdly. The source for a page returns the name “Trentemøller” as a regular string with a unicode character:
Trentem\u00f8ller
When I try to print that or commit it to a list print u'%s' % name or print unicode(name) it escape-sequences the backslash.
u'Trentem\\u00f8ller'
['foo', 'bar', u'Trentem\u00f8ller']
What is the proper way to treat this string? Ideally it would save it into the list in a u'' but not the added backslash.
If you’re in control of forming the unicode string, then use just one backslash:
If the regular string has already been formed by the screen scaper, you will need to re-evaluate the string to transform the backslash escape sequences into a real unicode characters. The eval builtin would tempting, but it is safer to use ast.literal_eval instead: