Print python dictionary with utf8 values

I have a dictionary with utf8 string values. I need to print it without the characters \ xd1 , \ u0441 or u .

# -*- coding: utf-8 -*-

a = u'lang='

# prints: lang=
print(a)

mydict = {}
mydict['string'] = a
mydict2 = repr(mydict).decode("unicode-escape")

# prints: {'string': u'lang='}
print mydict2

      

expected

{'string': 'lang='}

      

Is this possible without parsing a dictionary? This question is related to Python printing unicode strings in arrays as characters, not code points , but I need to get rid of this annoying u

+3


source to share


1 answer


I don't see a reasonable use case for this, but if you want to create a custom dictionary view (or better to say a custom unicode object view in the dictionary), you can do it yourself:

def repr_dict(d):
    return '{%s}' % ',\n'.join("'%s': '%s'" % pair for pair in d.iteritems())

      



and then

print repr_dict({u'string': u'lang='})

      

+2


source







All Articles