[Python-3000] Displaying strings containing unicode escapes
Oleg Broytmann
phd at phd.pp.ru
Wed Apr 16 15:30:46 CEST 2008
On Wed, Apr 16, 2008 at 11:21:26PM +1000, Nick Coghlan wrote:
> Hmm, the io module along with sys.stdout/err may be a better way to
> attack the problem then. Given:
>
> import sys, io
>
> class ParseUnicodeEscapes(io.TextIOWrapper):
> def write(self, text):
> super().write(text.encode('latin-1').decode('unicode_escape'))
>
> args = (sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors,
> None, sys.stdout.line_buffering)
>
> sys.stdout = ParseUnicodeEscapes(*args)
>
> args = (sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors,
> None, sys.stderr.line_buffering)
>
> sys.stderr = ParseUnicodeEscapes(*args)
>
> You get:
>
> >>> "тест"
> 'тест'
> >>> open("тест")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/ncoghlan/devel/py3k/Lib/io.py", line 212, in __new__
> return open(*args, **kwargs)
> File "/home/ncoghlan/devel/py3k/Lib/io.py", line 151, in open
> closefd)
> IOError: [Errno 2] No such file or directory: 'тест'
Very well, then. Thank you! The code should be put in a cookbook or the
wiki, if not in the library.
Oleg.
--
Oleg Broytmann http://phd.pp.ru/ phd at phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-3000
mailing list