Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100347
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "D'Arcy J.M. Cain" <darcy@VybeNetworks.com> |
| Newsgroups | comp.lang.python |
| Subject | codecs.StreamRecoder not doing what I expected. |
| Date | Sat, 12 Dec 2015 14:01:13 -0500 |
| Organization | Vybe Networks Inc. |
| Lines | 68 |
| Message-ID | <mailman.187.1449946891.12405.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de QOH2/4ZmUxLQd9aIBzrsbQjQGRcEvEg6LQpqZIXQn5tA== |
| Return-Path | <darcy@VybeNetworks.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'cents': 0.07; 'utf-8': 0.07; 'codecs': 0.09; 'exception,': 0.09; 'throws': 0.09; 'subject:not': 0.11; 'exception': 0.13; 'def': 0.13; '"r",': 0.16; 'codec': 0.16; 'decode': 0.16; 'netbsd': 0.16; 'ordinal': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'byte': 0.18; 'test.': 0.18; 'try:': 0.18; 'skip:" 30': 0.20; 'skip:" 40': 0.20; 'ascii': 0.22; 'trying': 0.22; 'import': 0.24; '(most': 0.24; 'plain': 0.24; 'installed': 0.26; 'skip:" 20': 0.26; 'handling': 0.27; "skip:' 10": 0.28; '7.0': 0.29; 'subject:what': 0.29; '15,': 0.30; 'another': 0.32; "can't": 0.32; 'skip:/ 20': 0.33; 'traceback': 0.33; 'file': 0.34; 'except': 0.34; 'running': 0.34; 'server': 0.34; 'skip:c 30': 0.35; 'unicode': 0.35; 'expected': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'skip:p 20': 0.38; 'files': 0.38; 'data': 0.39; 'sure': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'back': 0.62; 'more': 0.63; 'fall': 0.66; 'here': 0.66; '26,': 0.72; 'received:98.158': 0.84; 'subject:doing': 0.84; 'abc': 0.91 |
| X-Mailer | Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:100347 |
Show key headers only | View raw
More Unicode bafflement. What I am trying to do is pretty simple I
think. I have a bunch of files that I am pretty sure are either utf-8
or iso-8859-1. I try utf-8 and fall back to iso-8859-1 if it throws a
UnicodeError. Here is my test.
#! /usr/pkg/bin/python3.4
# Running on a NetBSD 7.0 server
# Installed with pkgsrc
import codecs
test_file = "StreamRecoder.txt"
def read_file(fn):
try: return open(fn, "r", encoding='utf-8').read()
except UnicodeError:
return codecs.StreamRecoder(open(fn),
codecs.getencoder('utf-8'),
codecs.getdecoder('utf-8'),
codecs.getreader('iso-8859-1'),
codecs.getwriter('iso-8859-1'), "r").read()
# plain ASCII
open(test_file, 'wb').write(b'abc - cents\n')
print(read_file(test_file))
# utf-8
open(test_file, 'wb').write(b'abc - \xc2\xa2\n')
print(read_file(test_file))
# iso-8859-1
open(test_file, 'wb').write(b'abc - \xa2\n')
print(read_file(test_file))
I expected all three to return UTF-8 strings but here is my output:
abc - cents
abc - ยข
Traceback (most recent call last):
File "./StreamRecoder_test", line 9, in read_file
try: return open(fn, "r", encoding='utf-8').read()
File "/usr/pkg/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 6:
invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./StreamRecoder_test", line 27, in <module>
print(read_file(test_file))
File "./StreamRecoder_test", line 15, in read_file
codecs.getwriter('iso-8859-1'), "r").read()
File "/usr/pkg/lib/python3.4/codecs.py", line 798, in read
data = self.reader.read(size)
File "/usr/pkg/lib/python3.4/codecs.py", line 489, in read
newdata = self.stream.read()
File "/usr/pkg/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa2 in position 6:
ordinal not in range(128)
--
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:darcy@Vex.Net VoIP: sip:darcy@VybeNetworks.com
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
codecs.StreamRecoder not doing what I expected. "D'Arcy J.M. Cain" <darcy@VybeNetworks.com> - 2015-12-12 14:01 -0500
csiph-web