简体   繁体   English

Python 2.7 base64.b64decode为什么它返回一个字符串而不是一个字节数组?

[英]Python 2.7 base64.b64decode why does it return a string and not a byte array?

I am using Python 2.7 to decode base64 data and I don't understand why base64.b64decode returns a string? 我使用Python 2.7解码base64数据,我不明白为什么base64.b64decode返回一个字符串? How do I get to the binary data that is decoded? 如何获取已解码的二进制数据? I would think base64.b64decode would return a byte array. 我认为base64.b64decode会返回一个字节数组。 Here is the link to python docs for base64: http://docs.python.org/2/library/base64.html 以下是base64的python文档链接: http//docs.python.org/2/library/base64.html

Thanks for the help! 谢谢您的帮助!

In 2.xa bytestring is the binary data, represented in a mostly-printable form. 在2.xa 字节串 二进制数据,在大多的可印刷形式表示。 And it doesn't require additional modules for support. 它不需要额外的模块来支持。

The bytearray type is used when we want mutable strings or arrays of bytes. 当我们想要可变字符串或字节数组时,使用bytearray类型。

The b64decode() function uses a str type which is for immutable strings or arrays of bytes in Python 2.7. b64decode()函数使用str类型,它用于Python 2.7中的不可变字符串或字节数组。

Besides not needing mutability, the other reason that b64decode() didn't consider returning a bytearray is that b64decode() is much older than bytearrays -- when the only tool in your toolbox is a str, all problems start to look like str problems ;-) 除了不需要易变性,另一个原因是b64decode()不考虑返回的ByteArray是b64decode()比的ByteArray老得多-当你的工具箱中的唯一工具是一个海峡,所有的问题开始看起来像海峡问题;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM