如何在python 3中将二进制数据写入stdout?

问题描述

更好的方法

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

解决方法

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

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

但是现在,我得到了TypeError: can't write bytes to text stream。我应该使用一些秘密编码吗?