Prev: modifying openoffice/xls spreadsheet (without OO /Excel installed)
Next: ooolib, reading writing a spread sheet and keep formatting
From: Dodo on 28 Apr 2010 17:54 Help! this is driving me crazy lol I want to print raw binary data to display an image file BUT python3 outputs b'<binary data>' instead of <binary data>.... so the browser can't read the image!! f = open("/some/path/%s" % x, 'rb') print(f.read()) any idea? Dorian
From: Gary Herron on 28 Apr 2010 18:08 Dodo wrote: > Help! this is driving me crazy lol > I want to print raw binary data to display an image file > BUT > python3 outputs b'<binary data>' instead of <binary data>.... so the > browser can't read the image!! > > f = open("/some/path/%s" % x, 'rb') > print(f.read()) > > > > any idea? > Dorian Huh??? In what universe does printing raw binary data display an image? But it that's what you want -- don't use print (which formats objects for human reading), and don't use standard output (which is a text stream in Python3). Instead, open your own stream in binary mode, and write (not print) to that stream.
From: Antoine Pitrou on 28 Apr 2010 19:45 Le Wed, 28 Apr 2010 23:54:07 +0200, Dodo a écrit : > Help! this is driving me crazy lol > I want to print raw binary data to display an image file BUT > python3 outputs b'<binary data>' instead of <binary data>.... so the > browser can't read the image!! > > f = open("/some/path/%s" % x, 'rb') > print(f.read()) print() implicitly converts its arguments to str (i.e. unicode strings) and then writes them to sys.stdout, which is a text IO wrapper. If you want to bypass the unicode layer, you have to use sys.stdout.buffer instead. That is: sys.stdout.buffer.write(f.read()) Regards Antoine.
From: Dodo on 29 Apr 2010 06:53 Le 29/04/2010 01:45, Antoine Pitrou a écrit : > Le Wed, 28 Apr 2010 23:54:07 +0200, Dodo a écrit : > >> Help! this is driving me crazy lol >> I want to print raw binary data to display an image file BUT >> python3 outputs b'<binary data>' instead of<binary data>.... so the >> browser can't read the image!! >> >> f = open("/some/path/%s" % x, 'rb') >> print(f.read()) > > print() implicitly converts its arguments to str (i.e. unicode strings) > and then writes them to sys.stdout, which is a text IO wrapper. > If you want to bypass the unicode layer, you have to use > sys.stdout.buffer instead. > That is: > > sys.stdout.buffer.write(f.read()) > > Regards > > Antoine. > @Gary : How do I reply to a http request then? @Antoine : It not sys.stdout.buffer.write but sys.stdout.write() instead. But it still doesn't work, now I have empty content #!/usr/bin/python3 import cgi, os, sys form = cgi.FieldStorage() all = os.listdir("/some/path/with/files/") for x in all: if x.find( form['id'].value ) != -1: ext = x.split(".")[-1] print("Content-type:image/%s\n\n" % ext) f = open("/some/path/with/files/%s" % x, 'rb') sys.stdout.write( f.read() ) f.close() break Dorian
From: Antoine Pitrou on 29 Apr 2010 11:07
Le Thu, 29 Apr 2010 12:53:53 +0200, Dodo a écrit : > > @Antoine : It not sys.stdout.buffer.write but sys.stdout.write() > instead. But it still doesn't work, now I have empty content Let me insist: please use sys.stdout.buffer.write(). You'll also have to call sys.stdout.flush() before doing so. |