Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 17th, 2006, 04:55 AM
BenjaMinster
Guest
 
Posts: n/a
Default Smarter way to do this? Unicode + stdin, stdout

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.

  #2  
Old December 17th, 2006, 09:25 AM
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles