【问题标题】:Is it normal for python's io.BytesIO.getvalue() to return str instead of bytes?python 的 io.BytesIO.getvalue() 返回 str 而不是 bytes 是否正常?
【发布时间】:2011-06-25 17:16:06
【问题描述】:

python的io.BytesIO.getvalue()返回str而不是bytes正常吗?

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

我应该提交错误吗?

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    不,这不是错误。这是正常行为。看到这个答案:the bytes type in python 2.7 and PEP-358

    它基本上归结为 2.7 bytes 只是 str 的别名,以平滑过渡到 3.x。

    【讨论】:

      【解决方案2】:

      bytes 在 Python 2.X 中不作为一种单独的数据结构存在,所以是的,这是完全正常的 - str 是 Python 2 中的字节字符串(与 Python 3 不同,其中 str 是 unicode 字符串) .

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 2022-08-21
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多