2

I have dictionary with utf8 string values. I need to print it without any \xd1 , \u0441 or u'string' symbols.

# -*- 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 it possible without parsing the dictionary? This question is related with Python print unicode strings in arrays as characters, not code points , but I need to get rid from that annoying u

Community
  • 1
  • 1
deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • 2
    Why do you need to produce that output? Python containers use `repr()` for the contents, and that means that Unicode values are shown with non-ASCII and non-printable characters are shown with escape sequences and with the `u` prefix. Don't use `repr()` if you don't want that display but loop over the contents yourself.. – Martijn Pieters Aug 04 '14 at 12:29
  • 1
    Is switching to Python 3 an option? Where all strings are Unicode strings and the `u` prefix isn't needed anymore? – Tim Pietzcker Aug 04 '14 at 12:35

1 Answers1

3

I can't see a reasonable use case for this, but if you want a custom representation of a dictionary (or better said, a custom representation of a unicode object within a dictionary), you can roll 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=русский'})
bgusach
  • 14,527
  • 14
  • 51
  • 68