2

コードのすべての行の前にある「u」の意味と、それらを削除する方法を知りたいですか? 私はパイソンで働いています。

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': ''},
4

4 に答える 4

7

uUnicode 文字列を作成するために使用されます。

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

次を使用してユニコード文字列を変換できますstr

>>> 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)

Python の Unicode 文字列について詳しくは、http://docs.python.org/2/howto/unicode.htmlを参照してください。

于 2013-07-01T14:15:49.353 に答える
5

uUnicode 文字列であることを意味します。文字列に 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?

于 2013-07-01T14:12:24.287 に答える
4

文字列の前の小文字uは、それが Unicode 文字列であることを意味します。これはエンコーディングのみであるため、まったく害はありません。£Unicode 文字列は、通常の文字列よりもさまざまな文字 ( など) を表すことができ、 suには表示されません。print

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

Python ドキュメントから Unicode 文字列の詳細を学ぶことができます: http://docs.python.org/3/howto/unicode.html

于 2013-07-01T14:12:42.847 に答える
2

Unicode を削除するには、型キャストを使用します。

    >>> x = u'abcd'
    >>> type(x)
    <type 'unicode'>
    >>> y = str(x)
    >>> type(y)
    <type 'str'>
于 2013-07-01T14:19:53.417 に答える