【发布时间】:2015-02-22 20:31:25
【问题描述】:
我是 python 新手,在我的 python 控制台中显示一些奇怪的东西,同时将输出显示到屏幕上。
>>> macbeth_sentsence = gutenberg.sents('shakespeare-macbeth.txt');
>>> macbeth_sentsence
[[u'[', u'The', u'Tragedie', u'of', u'Macbeth', u'by', u'William', u'Shakespeare', u'1603', u']'], [u'Actus', u'Primus', u'.'], ...]
我不希望在我的输出屏幕中出现额外的 'u' 字符。
任何人都知道如何抑制它?与默认 python 设置有什么关系?
以下更新:适用于不了解我要解决的问题的人。
当我的Windows系统在Vbox中执行相同的命令时
我得到了这样的东西:
>>> macbeth_sentsence = gutenberg.sents('shakespeare-macbeth.txt');
>>> macbeth_sentsence
[['[', 'The', 'Tragedie', 'of', 'Macbeth', 'by', 'William', 'Shakespeare', '1603', ']'], ['Actus', 'Primus', '.'], ...]
我想在我的 Mac 机器上得到同样的结果;我必须对我的默认值进行什么调整才能获得像我在 Windows 中得到的结果。
PS: 来自此的答案:removing `u` character in python output 这个:Python ascii utf unicode 而这个:Python string prints as [u'String'] 不是我想要的。
【问题讨论】:
-
这只是意味着它是一个 unicode 字符串。如果您实际使用
print打印字符串,您不会看到。 -
这是一个unicode
u,你为什么要删除它们? -
您看到的是
repr()表示,它为您提供了一种格式,可让您在新的 Python 会话中重现该值。总是有助于调试。 -
@PadraicCunningham 是的,我明白这一点,但为什么它只显示给我?我在其他同行屏幕上看不到这些东西,而只有我的。 @larsks~ 好的,是的,但是我该如何删除它们?与默认值有什么关系?
-
@MartijnPieters 很高兴知道这一点,但我现在没有调试,你能告诉我如何抑制它并仅在需要时使用它吗?
标签: python