【问题标题】:Why is there a 'u' before every line of my output? [duplicate]为什么在我的输出的每一行之前都有一个“u”? [复制]
【发布时间】:2013-06-28 16:42:06
【问题描述】:

只是想知道在我的代码的每一行之前的“u”的意义是什么,以及我如何能够删除它们?我在 python 中工作。

Last login: Mon Jul  1 09:58:27 on ttys000
Samuel-Finegolds-MacBook-Pro:~ samuelfinegold$ /var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup\ At\ Startup/tutor-394379967.500.py.command ; exit;
{u'company': {u'address': {u'city': u'Chicago',
                           u'contactname': '',
                           u'geo': {u'latitude': u'41.92113',
                                    u'longitude': u'-87.70085'},
                           u'state': u'IL',
                           u'street_address': '',
                           u'zip': u'60647'},
              u'companyname': u'Wyzant',
              u'costtype': '',
              u'description': u'WyzAnt is the leading tutoring marketplace on the web with 67,000+ tutors offering private lessons in hundreds of subjects like math, science, test prep, foreign languages, music, computers and much more.',
              u'email': '',
              u'facebook': u'https://www.facebook.com/WyzAnt',
              u'image': '',
              u'language': '',
              u'linkedin': '',
              u'logo': '',
              u'phone': u'8779992681',
              u'program': {u'costrange': u'[]',
                           u'costtype': '',
                           u'programtype': ''},

【问题讨论】:

标签: python json


【解决方案1】:

u 用于创建 unicode 字符串:

>>> unicode_string = u'my unicode string'
>>> type(unicode_string)
<type 'unicode'>
>>> ascii_string = 'my ascii string'
>>> type(ascii_string)
<type 'str'>

您可以使用 str 转换 unicode 字符串:

>>> converted_string = str(unicode_string)
>>> type(converted_string)

但是,只有当您的 unicode 字符串中的字符可以使用 ascii 表示时,这才有可能:

>>> unicode_string = u'ö'
>>> converted_string = str(unicode_string)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 0: ordinal not in range(128)

您可以在http://docs.python.org/2/howto/unicode.html阅读更多关于 Python 的 unicode 字符串的信息

【讨论】:

    【解决方案2】:

    u 表示它是一个 unicode 字符串,如果该字符串仅包含 ASCII 字符则无需转换为普通的str 为:

    >>> "foo" == u"foo"
    True
    

    但你不能将 unicode 字符串与包含非 ASCII 字符的字节字符串进行比较:

    >>> u'ö' == 'ö'
    False
    >>> 'ö'       #contains bytes
    '\xc3\xb6'
    >>> u'ö'      #contains sequence of code-points 
    u'\xf6'
    

    只有将字节字符串转换为unicode(使用正确的编码)才能进行比较:

    >>> u'ö' == 'ö'.decode('utf-8')
    True
    

    文档:Unicode HOWTO

    Ned Batchelder 的 ppt:Pragmatic Unicode : How Do I Stop the Pain?

    【讨论】:

    • @downvoter 解释您的反对意见,以便我改进我的回答。
    • 没有否决你,但“不会以任何方式影响你的输出”你不觉得有点牵强吗?
    • @rantanplan 你能举个例子说明我的句子可能是错的吗?
    • 执行以下操作:转到您的终端并在非 ascii 范围内(希腊语、俄语等)创建 2 个包含相同字符串的变量。在一个字符串中,您将在前面加上u,而在另一个字符串中则不会。然后将它们与相等测试进行比较。它们看起来一样……但完全不同!
    • @rantanplan 感谢您的建设性批评。 :)
    【解决方案3】:

    字符串前面的小写u表示它是一个unicode字符串。 这只是编码,因此根本没有害处。 Unicode 字符串能够表示比普通字符串更广泛的字符(例如£),u 不会显示在prints 中:

    >>> print(u'hi')
    'hi'
    

    您可以从 python 文档中了解更多关于 unicode 字符串的信息:http://docs.python.org/3/howto/unicode.html

    【讨论】:

    • “这只是编码,因此没有任何危害”。你是认真的人吗?这里的“它只是编码”模因是什么?编码是奇怪问题的根源。不要轻描淡写。大家应该研究一下bytestings和unicode的区别。
    【解决方案4】:

    要删除 unicode,请使用类型转换。

        >>> x = u'abcd'
        >>> type(x)
        <type 'unicode'>
        >>> y = str(x)
        >>> type(y)
        <type 'str'>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2014-02-08
      • 2013-04-18
      • 1970-01-01
      • 2020-02-07
      • 2014-02-07
      相关资源
      最近更新 更多