python的io.BytesIO.getvalue()返回str而不是字节,这正常吗?

2024-04-18 18:14:41 发布

您现在位置:Python中文网/ 问答频道 /正文

python的io.BytesIO.getvalue()返回str而不是字节是否正常?

 Python 2.7.1 (r271:86832, Jun 13 2011, 14:28:51) 
 >>> import io
 >>> a = io.BytesIO()
 >>> a
 <_io.BytesIO object at 0x10f9453b0>
 >>> a.getvalue()
 ''
 >>> print type(a.getvalue())
 <type 'str'>
 >>> 

我应该提出一个错误吗?


Tags: ioimport字节objecttype错误junat
2条回答

不,你不应该提出一个错误。这是正常的行为。看这个答案:the bytes type in python 2.7 and PEP-358

基本上,2.7 bytes只是str平滑过渡到3.x的别名

bytes在Python 2.X中不作为一种独立的数据结构存在,所以是的,这是完全正常的-str是python2中的bytestrings(不像Python 3中的str是unicode字符串)。

相关问题 更多 >