将 utf-8 字符串拆分为 python 中的字节
split utf-8 string into bytes in python
我正在尝试将 UTF-8 字符串拆分为 python 中的字节 3. 问题是,当我使用 bytearray、byte、encode 等函数时,我总是得到一个元素大小为 14 字节的数组,而不是我预期的 1 个字节。我需要将任何文本文件拆分为字节序列并使用套接字逐字节发送它们。我试过这样的事情:
infile = open (file, "r")
str = infile.read()
byte_str = bytes(str, 'UTF-8')
print("size of byte_str",sys.getsizeof(byte_str[0]))
打印给我 14,但我需要 1...有什么建议吗?
sys.getsizeof(object[, default])
Return the size of an object in bytes. The object can be any type of
object. All built-in objects will return correct results, but this
does not have to hold true for third-party extensions as it is
implementation specific.
Only the memory consumption directly attributed to the object is
accounted for, not the memory consumption of objects it refers to.
If given, default will be returned if the object does not provide
means to retrieve the size. Otherwise a TypeError will be raised.
getsizeof() calls the object’s __sizeof__
method and adds an
additional garbage collector overhead if the object is managed by the
garbage collector.
See recursive sizeof recipe for an example of using getsizeof()
recursively to find the size of containers and all their contents.
我正在尝试将 UTF-8 字符串拆分为 python 中的字节 3. 问题是,当我使用 bytearray、byte、encode 等函数时,我总是得到一个元素大小为 14 字节的数组,而不是我预期的 1 个字节。我需要将任何文本文件拆分为字节序列并使用套接字逐字节发送它们。我试过这样的事情:
infile = open (file, "r")
str = infile.read()
byte_str = bytes(str, 'UTF-8')
print("size of byte_str",sys.getsizeof(byte_str[0]))
打印给我 14,但我需要 1...有什么建议吗?
sys.getsizeof(object[, default])
Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.
Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.
If given, default will be returned if the object does not provide means to retrieve the size. Otherwise a TypeError will be raised.
getsizeof() calls the object’s
__sizeof__
method and adds an additional garbage collector overhead if the object is managed by the garbage collector.See recursive sizeof recipe for an example of using getsizeof() recursively to find the size of containers and all their contents.