Regards, Peter
--
Peter Billam www.pjb.com.au www.pjb.com.au/comp/contact.html
> Greetings. (Newbie warning as usual) In Python3, sys.stdout is a
> io.TextIOWrapper object; but I want to output bytes
> (e.g. muscript -midi t > t.mid )
> and they're coming out stringified :-( How can I either change the
> encoding on sys.stdout, or close sys.stdout and reopen it in 'b'
> mode, or dup(fd) it first, or whatever the right thing to do is ?
I cannot test this with Python 3 right now, but I think this should work
(right at the start of your script):
sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb')
--
Gabriel Genellina
The official API to write binary data to stdout is:
>>> count = sys.stdout.buffer.write(b"abc\n")
abc
Christian
Is this really the 'official' way to do this? This isn't meant to be
confrontational or trolling; I honestly don't know the answer and I
had similar questions when I first started with the 3.0 release
candidates and I have yet to find a good answer in the Python v3.0
documentation. Why wouldn't you just use:
print(bytes.decode(b'abc\n'), end='')
That's the one that works the best for me :-)
Thanks for all the suggestions!
Yes, it's really the official way. You can google up the discussion
between me and Guido on the python-dev list if you don't trust me. ;)
The docs concur with me, too.
http://docs.python.org/3.0/library/sys.html#sys.stdin
Note: The standard streams are in text mode by default. To write or read
binary data to these, use the underlying binary buffer. For example, to
write bytes to stdout, use sys.stdout.buffer.write(b'abc').
> Why wouldn't you just use:
>
> print(bytes.decode(b'abc\n'), end='')
Because it doesn't work with binary data. You can't convert random bytes
to unicode. Try that with a JPEG file or even the byte sequence that
contains some invalid UTF-8 chars.
>>> bytes((255, 255, 255))
b'\xff\xff\xff'
>>> bytes((255, 255, 255)).decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
unexpected code byte
Christian
Thanks! Incidentally, the 'Note:' section you reference in the HTML
docs doesn't appear to be in the .CHM file that installs with the
windows version of 3.01. That would have made my search a lot
easier :-%
Because that code is incapable of sending bytes that cannot be
interpreted as encoded in the "default" encoding. If you are
sending a picture, for example, all possible byte sequences might
show up. If the default encoding is "utf-8", for example, there
are byte sequences which are illegal. I suspect you are confused
by thinking each byte must map to a character; such an encoding
could never suffice for Chinese, for example.
--Scott David Daniels
Scott....@Acm.Org
Oh, that should not happen. Please report this bug!
Christian