Connecting Tech Pros Worldwide Forums | Help | Site Map

Smarter way to do this? Unicode + stdin, stdout

BenjaMinster
Guest
 
Posts: n/a
#1: Dec 17 '06
I want to read and write unicode on stdin and stdout. I can't seem to
find any way to force sys.stdin.encoding and sys.stdout.encoding to be
utf-8, so I've got the following workaround:

import codecs, sys
out = codecs.getwriter("utf-8")(sys.stdout)

def tricky(): return sys.stdin.readline().decode("utf-8").[:-1]

That is, I wrap sys.stdout in a utf-8 writer and I read utf-8 input
from stdin as normal characters then convert it to
unicode and chop off the newline. Wrapping stdin with
codecs.getreader() doesn't work for some reason--when I call readline()
on the resulting wrapped stream, it won't return until I send it Ctrl+d
twice! Then stdin is closed which is of course not helpful.


=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
 
Posts: n/a
#2: Dec 17 '06

re: Smarter way to do this? Unicode + stdin, stdout


BenjaMinster schrieb:
Quote:
I want to read and write unicode on stdin and stdout. I can't seem to
find any way to force sys.stdin.encoding and sys.stdout.encoding to be
utf-8, so I've got the following workaround:
What operating system are you using? Why do you want to do this?
Python attempts to determine the encoding of your terminal (if
sys.stdout is a terminal), and set sys.stdout.encoding accordingly.

Regards,
Martin
Closed Thread