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


Groups > comp.lang.python > #81794

Re: [Python-Dev] Unicode decode exception

X-Received by 10.67.4.103 with SMTP id cd7mr21053198pad.8.1417505307179; Mon, 01 Dec 2014 23:28:27 -0800 (PST)
X-Received by 10.140.91.35 with SMTP id y32mr3880qgd.25.1417505307122; Mon, 01 Dec 2014 23:28:27 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!h15no11787732igd.0!news-out.google.com!m4ni588qag.1!nntp.google.com!s7no3525988qap.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Mon, 1 Dec 2014 23:28:27 -0800 (PST)
In-Reply-To <mailman.16446.1417408427.18130.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=2a02:120b:c3c5:8370:7117:5a88:6b9d:2351; posting-account=ung4FAoAAAC46zhHJ0Nsnuox7M5gDvs_
NNTP-Posting-Host 2a02:120b:c3c5:8370:7117:5a88:6b9d:2351
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>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <0aebbb26-93ea-4fc0-8259-e93b93c40c61@googlegroups.com> (permalink)
Subject Re: [Python-Dev] Unicode decode exception
From wxjmfauth@gmail.com
Injection-Date Tue, 02 Dec 2014 07:28:27 +0000
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:81794

Show key headers only | 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