[Python-3000] Displaying strings containing unicode escapes at the interactive prompt
Nick Coghlan
ncoghlan at gmail.com
Wed Apr 16 15:21:26 CEST 2008
Oleg Broytmann wrote:
> On Wed, Apr 16, 2008 at 10:11:13PM +1000, Nick Coghlan wrote:
>> atsuo ishimoto wrote:
>>> IOError: [Errno 2] No such file or directory: '\u65e5\u672c\u8a9e'
>> This is starting to seem to me more like something to be addressed
>> through sys.displayhook/excepthook at the interactive interpreter level
>
> The problem manifests itself in scripts, too:
>
> Traceback (most recent call last):
> File "./ttt.py", line 4, in <module>
> open("тест") # filename is in koi8-r encoding
> IOError: [Errno 2] No such file or directory: '\xd4\xc5\xd3\xd4'
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: 'тест'
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list