Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81794

Re: [Python-Dev] Unicode decode exception

Newsgroups comp.lang.python
Date 2014-12-01 23:28 -0800
References <CAKcrOwft6+6TBCpcLoG8Z4fcbpjcECUmNd4Wu-NhRbHDwr1V-g@mail.gmail.com> <CAPTjJmqbUm8g0qszy-RamDQrDQgb1O3ievY+86dRJO=9Jr5weA@mail.gmail.com> <CAKcrOweVZ4-KUpW4a428zO_3_43-n5HA=YnXrAuFx3KyWkcmSA@mail.gmail.com> <mailman.16446.1417408427.18130.python-list@python.org>
Message-ID <0aebbb26-93ea-4fc0-8259-e93b93c40c61@googlegroups.com> (permalink)
Subject Re: [Python-Dev] Unicode decode exception
From wxjmfauth@gmail.com

Show all headers | View raw


>>> 
>>> # input
>>> source_ascii = b'\x61\x62\x63'
>>> source_greek = b'\xe1\xe2\xe3'
>>> source_cyrillic = b'\xd0\xd1\xd2'
>>> buffer = ''
>>> buffer  = buffer + source_ascii.decode('ascii')
>>> buffer  = buffer + source_greek.decode('cp1253')
>>> buffer  = buffer + source_cyrillic.decode('iso-8859-5')
>>> len(buffer)
9
>>> # output
>>> sys.stdout.sethostencoding('cp1252')
>>> print(buffer.encode(sys.stdout.encoding, 'replace').\
...     decode(sys.stdout.encoding))
abc??????
>>> sys.stdout.sethostencoding('mac-roman')
>>> print(buffer.encode(sys.stdout.encoding, 'replace').\
...     decode(sys.stdout.encoding))
abc??????
>>> sys.stdout.sethostencoding('ascii')
>>> print(buffer.encode(sys.stdout.encoding, 'replace').\
...     decode(sys.stdout.encoding))
abc??????
>>> sys.stdout.sethostencoding('cp1253')
>>> print(buffer.encode(sys.stdout.encoding, 'replace').\
...     decode(sys.stdout.encoding))
abcαβγ???
>>> sys.stdout.sethostencoding('iso-8859-5')
>>> print(buffer.encode(sys.stdout.encoding, 'replace').\
...     decode(sys.stdout.encoding))
abc???абв
>>> sys.stdout.sethostencoding('utf-32-be')
>>> print(buffer.encode(sys.stdout.encoding, 'replace').\
...     decode(sys.stdout.encoding))
abcαβγабв
>>> 
>>> # typical decode error
>>> source_greek.decode('ascii')
Traceback (most recent call last):
  File "<eta last command>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 0: ordinal not in 
range(128)

From a *Py32* gui interactive interpreter mimicking
platforms.

jmf

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: [Python-Dev] Unicode decode exception Michael Torrie <torriem@gmail.com> - 2014-11-30 21:33 -0700
  Re: [Python-Dev] Unicode decode exception wxjmfauth@gmail.com - 2014-12-01 23:28 -0800

csiph-web