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

2
  • 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.. Commented Aug 4, 2014 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? Commented Aug 4, 2014 at 12:35

1 Answer 1

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=русский'})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.