error writing str to binary stream - fails in Python 3.0.1, works in 2.x

Benjamin Peterson benjamin at python.org
Mon Mar 16 22:10:45 CET 2009


 <wallenpb <at> gmail.com> writes:
> 
> self.out.write(b'BM') worked beautifully.  Now I also have a similar
> issue, for instance:
> self.out.write("%c" % y) is also giving me the same error as the other
> statement did.
> I tried self.out.write(bytes("%c" %y),encoding=utf-8) in an attempt to
> convert to bytes, which it did, but not binary.  How do I affect
> self.out.write("%c" % y) to write out as a binary byte steam?  I also
> tried self.out.write(b"%c" % y), but b was an illegal operator in when
> used that way.
> It is also supposed to be data being written to the .bmp file. --Bill

Are you writing to sys.stdout? If so, use sys.stdout.buffer.write(b'some
bytes'). If you're writing to a file, you have to open it in binary mode like
this: open("someimage.bmp", "wb") 







More information about the Python-list mailing list