【问题标题】:What is the "u" in results (Python)? [duplicate]结果(Python)中的“u”是什么? [复制]
【发布时间】:2016-07-13 23:12:30
【问题描述】:

我正在阅读《Python Text Processing with NLTK》这本书,书上的结果是:

>>> stopwords.fileids()
['danish', 'dutch', 'english', 'finnish', 'french', 'german', 'hungarian', 'italian', 'norwegian', 'portuguese', 'russian', 'spanish', 'swedish', 'turkish']

但是当我在终端运行代码时,结果是:

>>> stopwords.fileids()
[u'danish', u'dutch', u'english', u'finnish', u'french', u'german', u'hungarian', u'italian', u'norwegian', u'portuguese', u'russian', u'spanish', u'swedish', u'turkish']

每个字符串前面的“u”是什么?

【问题讨论】:

  • 比较两个字符串,一个带前缀 'u' 而另一个不带前缀,仍然会返回 True(完美匹配),因此在许多情况下您无需担心。
  • 您当前使用的是 Python 2。您希望切换到 Python 3 进行自然语言处理,因为它具有出色的文本处理能力;这个u 前缀也没有了。 NLTK 3.0 版支持 Python 3。
  • @AnttiHaapala,如果用户使用带有 NLTK v. 2 的 Python 2,就不会有 unicode 前缀,对吗?她使用的文本中的结果(大概)是使用 Python 2 和 NLTK 2 生成的,这些结果显示 no unicode 前缀。
  • @Silenus 也许;但现在任何从事自然语言处理的人都应该同时使用 Python 3 和 NLTK 3。

标签: python nltk


【解决方案1】:

u 代表包含 unicode 的字符串

您可以通过在 python 解释器中输入以下内容自行检查:

s = unicode('abcdef')
type(s) # <type 'unicode'>
t = u'unicode'
type(t) #<type 'unicode'>

关于 unicode 字符串的更多信息python2 | python3

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 2021-02-24
    • 2017-02-20
    • 2011-05-04
    • 1970-01-01
    • 2013-10-02
    • 2023-03-31
    • 2014-01-13
    • 1970-01-01
    相关资源
    最近更新 更多