gpt4 book ai didi

python - 字符串文字 Vs Unicode 文字 Vs unicode 类型对象 - 内存表示

转载 作者:行者123 更新时间:2023-11-30 22:35:59 25 4
gpt4 key购买 nike

Python 2.x doc说,

Unicode string is a sequence of code points

Unicode strings are expressed as instances of the unicode type

>>> ThisisNotUnicodeString = 'a정정💛' # What is the memory representation?
>>> ThisisNotUnicodeString
'a\xec\xa0\x95\xec\xa0\x95\xf0\x9f\x92\x9b'
>>> type(ThisisNotUnicodeString)
<type 'str'>
>>> a = u'a정정💛' # Which encoding technique used to represent in memory? utf-8?
>>> a
u'a\uc815\uc815\U0001f49b'
>>> type(a)
<type 'unicode'>
>>> b = unicode('a정정💛', 'utf-8')
>>> b
u'a\uc815\uc815\U0001f49b'
>>> c = unicode('a정정💛', 'utf-16')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/encodings/utf_16.py", line 16, in decode
return codecs.utf_16_decode(input, errors, True)
UnicodeDecodeError: 'utf16' codec can't decode byte 0x9b in position 10: truncated data
>>>
<小时/>

问题:

1) ThisisNotUnicodeString 是字符串文字。尽管 ThisisNotUnicodeString 不是 unicode 文字,但使用哪种编码技术在内存中表示 ThisisNotUnicodeString ?因为应该有一些编码技术来表示内存中的 💛 字符。

2) 使用哪种编码技术在内存中表示 unicode 文字 a? utf-8?如果是,如何知道占用的字节数?

3) 为什么使用utf-16技术在内存中不表示c

最佳答案

1) ThisisNotUnicodeString is string literal. Despite ThisisNotUnicodeString is not a unicode literal, Which encoding technique used to represent ThisisNotUnicodeString in memory? Because there should be some encoding technique to represent 정 or 💛 character in memory.

在交互式提示符中,Python 2.X 的 str 类型将使用哪种编码取决于您的 shell 编码,例如如果您在 Linux 系统下运行终端,编码为终端为 UTF-8:

>>> s = "a정정💛"
>>> s
'a\xec\xa0\x95\xec\xa0\x95\xf0\x9f\x92\x9b'

现在尝试将终端窗口的编码更改为其他内容,在本例中,我已将 shell 的编码从 UTF-8 更改为 WINDOWS-1250:

 >>> s = "a???"

如果您在 tty session 中尝试此操作,您会得到钻石而不是 ?至少在Ubuntu下你可能会得到不同的字符。

正如您可以得出的结论,将使用哪种编码来确定交互式提示中 str 的编码取决于 shell。这适用于在 Python 解释器下交互式运行的代码,不交互式运行的代码将引发异常:

#main.py
s = "a정정💛"

尝试运行代码会引发SynatxError:

$ python main.py
SyntaxError: Non-ASCII character '\xec' in file main.py...

这是因为 Python 2.X 默认使用 ASCII:

>>> sys.getdefaultencoding()
'ascii'

然后,您必须通过执行以下操作在代码中显式指定编码:

#main.py
#*-*encoding:utf-8*-*
s = "a정정💛"

2) Which encoding technique used to represent unicode literal a in memory? utf-8? If yes, How to know the number of bytes occupied?

请记住,如果您在不同的 shell 中运行代码,编码方案可能会有所不同,我已在 Linux 下对此进行了测试,这对于 Windows 可能略有不同,因此请检查您的操作系统的文档。

要了解占用的字节数,请使用len:

>>> s = "a정정💛"
>>> len(s)
11

s 正好占用 11 个字节。

2) Which encoding technique used to represent unicode literal a in memory? utf-8? If yes, How to know the number of bytes occupied?

好吧,这是一个困惑,unicode 类型没有编码。它只是一个 Unicode 字符点序列(也称为 Commercial At 的 U+0040)。

3) Why c is not represented in memory, using utf-16 technique?

UTF-8 是一种与 UTF-16 不同的编码方案——UTF-8 表示字符字节的方式与 UTF-16 不同。这里:

>>> c = unicode('a정정💛', 'utf-16')

你实际上是在这样做:

>>> "a정정💛"
'a\xec\xa0\x95\xec\xa0\x95\xf0\x9f\x92\x9b'
>>> unicode('a\xec\xa0\x95\xec\xa0\x95\xf0\x9f\x92\x9b', 'utf-16')
UnicodeDecodeError: 'utf16' codec can't decode byte 0x9b in position 10: truncated data

这是因为您正在尝试使用 UTF-16 来解码 UTF-8。同样,两者都使用不同数量的字节来表示字符,它们只是两种不同的编码方案——以字节表示字符的不同方式。

供您引用: Python str vs unicode types

关于python - 字符串文字 Vs Unicode 文字 Vs unicode 类型对象 - 内存表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44351350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com