When your file contains the line
foo = "Gro\xdfbritannien"
it contains an actual backslash character, followed by x
, d
and f
. So if that line is read into a Python string, it is read as
'foo = "Gro\\xdfbritannien"'
(and since those are all ASCII characters, it doesn't matter if you open it with the utf-8
codec or not).
So you need to decode it first using the string_escape
codec:
>>> foo.decode("string_escape")
'Gro\xdfbritannien'
and then decode it to the correct Unicode object
>>> _.decode("latin1")
u'Gro\xdfbritannien'
which you can then print
>>> print _
Großbritannien