有没有办法在 python 中全局抑制 unicode 字符串指示符?我在一个应用程序中专门使用 unicode,并且做了很多交互的东西。在我的所有调试输出中显示 u'prefix' 是不必要且令人讨厌的。可以关掉吗?
最佳答案
您可以使用 Python 3.0.. 默认字符串类型是 unicode,因此不再需要 u''
前缀..
简而言之,没有。您无法关闭此功能。
u
来自 unicode.__repr__
方法,用于在 REPL 中显示东西:
>>> print repr(unicode('a'))
u'a'
>>> unicode('a')
u'a'
如果我没记错的话,你不能在不重新编译 Python 的情况下覆盖它。
解决这个问题的最简单方法是简单地打印字符串..
>>> print unicode('a')
a
如果你使用 unicode()
内置函数来构造你的所有字符串,你可以这样做......
>>> class unicode(unicode):
... def __repr__(self):
... return __builtins__.unicode.__repr__(self).lstrip("u")
...
>>> unicode('a')
a
..但不要那样做,太可怕了
关于python - 抑制python字符串中的u'前缀表示unicode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/761361/