Is there a way to globally suppress the unicode string indicator in python? I'm working exclusively with unicode in an application, and do a lot of interactive stuff. Having the u'prefix' show up in all of my debug output is unnecessary and obnoxious. Can it be turned off?
You could use Python 3.0.. The default string type is unicode, so the u''
prefix is no longer required..
In short, no. You cannot turn this off.
The u
comes from the unicode.__repr__
method, which is used to display stuff in REPL:
>>> print repr(unicode('a'))
u'a'
>>> unicode('a')
u'a'
If I'm not mistaken, you cannot override this without recompiling Python.
The simplest way around this is to simply print the the string..
>>> print unicode('a')
a
If you use the unicode()
builtin to construct all your strings, you could do something like..
>>> class unicode(unicode):
... def __repr__(self):
... return str(self)
...
>>> unicode('a')
a
..but don't do that, it's horrible
Try this trick at the start of your program:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
I know this isn't a global option, but you can also suppress the Unicode u by placing the string in a str() function.
So a Unicode derived list that would look like:
>>> myList=[unicode('a'),unicode('b'),unicode('c')]
>>> myList
[u'a', u'b', u'c']
would become this:
>>> myList=[str(unicode('a')),str(unicode('b')),str(unicode('c'))]
>>> myList
['a', 'b', 'c']
It's a bit cumbersome, but might be useful to some one