StringIO 替换适用于字节而不是字符串? [英] StringIO replacement that works with bytes instead of strings?

查看:103
本文介绍了StringIO 替换适用于字节而不是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python StringIO 类是否有任何替代品,可以使用 bytes 而不是字符串?

Is there any replacement for python StringIO class, one that will work with bytes instead of strings?

这可能并不明显,但如果您使用 StringIO 来处理二进制数据,那么 Python 2.7 或更高版本就不那么走运了.

It may not be obvious but if you used StringIO for processing binary data you are out of luck with Python 2.7 or newer.

推荐答案

尝试 io.BytesIO.

作为其他 指出,您确实可以在 2.7 中使用 StringIO,但 BytesIO 是向前兼容的好选择.

As others have pointed out, you can indeed use StringIO in 2.7, but BytesIO is a good choice for forward-compatibility.

这篇关于StringIO 替换适用于字节而不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆