On Mar 16, 4:10 pm, Benjamin Peterson <benja...@python.org> wrote: > <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")
Yes, I am writing to a file. That portion is correct and goes like this: self.out=open(filename,"wb") self.out.write(b"BM") # This line works thanks to advice given in previous reply However, here is some more code that is not working and the error it gives: def write_int(self,n): str_out='%c%c%c%c' % ((n&255),(n>>8)&255,(n>>16)&255,(n>>24)&255) self.out.write(str_out) #this is line 29, does not work - not sure how to get this complex str converted over to binary bytes to write to bmp file. #tried to use self.out.write(bytes ("blah", encoding='utf-8')) type coding here. This way ran without error, but #did not write proper data to the .bmp file.In hex editor it was obvious it was filling in wrong bits. #Also tried self.out.write(b'%c%c%c%c' % (blah)) but this gave errors for bad operands for % operator. --------------------- Traceback (most recent call last): File "C:\Python30\py_kohn_bmp.py", line 29, in write_int self.out.write(str_out) File "C:\Python30\lib\io.py", line 1038, in write raise TypeError("can't write str to binary stream") TypeError: can't write str to binary stream -- http://mail.python.org/mailman/listinfo/python-list