Re: [Python-ideas] changing sys.stdout encoding

On 06/05/2012 05:56 PM, MRAB wrote:
But is it really? The following is very simple and the level of python expertise required is minimal. It (would) works fine with redirection. One could substitute any other ordinary open (for write) text file for sys.stdout. [off the top of my head] text = 'This is %s text: 世界へ、こんにちは!' sys.stdout.set_encoding ('sjis') print (text % 'sjis') sys.stdout.set_encoding ('euc-jp') print (text % 'euc-jp') sys.stdout.set_encoding ('iso2022-jp') print (text % 'iso2022-jp') As for your suggestion, how do I reopen sys.stdout in binary mode? I don't need to do that often and don't know off the top of my head. (And it's too late for me to look it up.) And what happens to redirected output when I close and reopen the stream? I can open a regular filename instead. But remember to make the last two opens with "a" rather than "w". And don't forget the "\n" at the end of the text line. Could you show me an code example of your suggestion for comparison? Disclaimer: As I said before, I am not particularly advocating for a for a set_encoding() method -- my primary suggestion is a programatic way to change the sys.std* encodings prior to first use. Here I am just questioning the claim that a set_encoding() method would not be clearer than existing alternatives.
participants (1)
-
Rurpy