如何在python 3中将二进制数据写入stdout? [英] How to write binary data to stdout in python 3?

查看:79
本文介绍了如何在python 3中将二进制数据写入stdout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 2.x 中,我可以这样做:

In python 2.x I could do this:

import sys, array
a = array.array('B', range(100))
a.tofile(sys.stdout)

然而,现在我得到一个TypeError: can't write bytes to text stream.有什么我应该使用的秘密编码吗?

Now however, I get a TypeError: can't write bytes to text stream. Is there some secret encoding that I should use?

推荐答案

更好的方法:

import sys
sys.stdout.buffer.write(b"some binary data")

这篇关于如何在python 3中将二进制数据写入stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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