Python在显示任何内容之前给出输出'u'字符?

时间:2015-02-22 20:31:25

标签: python

我是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设置有什么关系?


从下面更新:对于那些不了解我想要解决的人。

在Vbox的Windows系统中执行相同的命令时

我有这样的事情:

>>> 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']不是我想要的。

2 个答案:

答案 0 :(得分:0)

它只是指示一个unicode字符串。 看到 Easiest way to remove unicode representations from a string in python 3?

答案 1 :(得分:0)

它是一个Unicode字符串,你可以通过转换为ascii来删除它:

macbeth_sentence = [[i.encode() for i in j] for j in macbeth_sentence]