I have a dictionary which looks like this.
{ latex schlüpfer : [455, 'latex schl\xc3\xbcpfer', 5.0, 0.0, 0.0, 24.6] }
Why is my list value at index 1 showing in a different encoding?
I've tried replacing it with the key as well as appending it and it always comes out the same.
Even when I go:
print key # latex schlüpfer
print [key] # latex schl\xc3\xbcpfer
What's going on?
I'm trying to check if the item exists but the encoding issues appear to be preventing me from doing this as I'm comparing latex schl\xc3\xbcpfer to latex schlüpfer
2 Answers 2
objects inside a list are printed using their __repr__ method. this reproduces your output:
# coding=utf-8
s = 'latex schlüpfer'
print(s)
print(repr(s))
it prints
latex schlüpfer
'latex schl\xc3\xbcpfer'
3 Comments
repr (repr is just a [short] string representation of an object; comparison calls these methods).You can try to encode your entire dictionary by using the follow code:
mydict = {k: unicode(v).encode("utf-8") for k,v in mydict.iteritems()}
This uses a dictionary comprehension.