问题描述
有没有办法全局抑制python中的unicode字符串指示符?我在一个应用程序中专门使用 unicode,并做了很多交互式的东西.让 u'prefix' 出现在我所有的调试输出中是不必要且令人讨厌的.可以关掉吗?
您可以使用 Python 3.0.. 默认字符串类型是 unicode,因此不再需要 u''
前缀..
简而言之,没有.您无法关闭此功能.
u
来自 unicode.__repr__
方法,用于在 REPL 中显示内容:
如果我没记错的话,你不能在不重新编译 Python 的情况下覆盖它.
解决这个问题的最简单方法是简单地打印字符串..
>>>打印 unicode('a')一种如果你使用 unicode()
内建来构造你的所有字符串,你可以做类似的事情..
...但是不要那样做,这太可怕了
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 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 __builtins__.unicode.__repr__(self).lstrip("u")
...
>>> unicode('a')
a
..but don't do that, it's horrible
这篇关于抑制python字符串中的u'前缀指示unicode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!