Python 如何获得字符串的大小?
我有一个TCP客户端-服务器通信。我正在发送字符串,我想测量包裹的大小,我怎么做 在我的情况下,这是不好的,因为Python 如何获得字符串的大小?,python,Python,我有一个TCP客户端-服务器通信。我正在发送字符串,我想测量包裹的大小,我怎么做 在我的情况下,这是不好的,因为 getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector. 一个字符的大小是多少,2字节还是4字节?在这种情况下,我可以简单地测量为msg_si
getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector
overhead if the object is managed by the garbage collector.
一个字符的大小是多少,2字节还是4字节?在这种情况下,我可以简单地测量为
msg_size=x*len(msg)
,但我不知道在Python2.x中有多少字节是x
,str
对象是8位的,即每个项1字节。在3.x中,您可以使用字节
对象,它们同样是每个项目1字节
对于处理数据,通常最好使用;这是可变的,在2.x和3.x中的作用相同。len(string)返回字符串使用的字节数(len(ustring)返回字符数)