Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: codecs.StreamRecoder not doing what I expected. Date: Sat, 12 Dec 2015 21:35:36 +0100 Organization: None Lines: 84 Message-ID: References: <20151212140113.78e3c945@imp> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de +k1nk2dz3Husnxy8mZ/jjgLPiFh3+hferMDDKLgZWCaA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.05; 'cents': 0.07; 'converts': 0.07; 'utf-8': 0.07; 'bytes,': 0.09; 'codecs': 0.09; 'exception,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'throws': 0.09; 'subject:not': 0.11; 'exception': 0.13; 'def': 0.13; 'encoding': 0.15; '"r",': 0.16; 'codec': 0.16; 'decode': 0.16; 'encodings': 0.16; 'f.read()': 0.16; 'netbsd': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'bytes': 0.18; 'test.': 0.18; 'try:': 0.18; 'skip:" 30': 0.20; 'skip:" 40': 0.20; 'ascii': 0.22; 'pass': 0.22; 'trying': 0.22; 'import': 0.24; '(most': 0.24; 'plain': 0.24; 'header:User- Agent:1': 0.26; 'installed': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'handling': 0.27; "skip:' 10": 0.28; '7.0': 0.29; 'mode.': 0.29; 'subject:what': 0.29; 'raise': 0.29; '15,': 0.30; 'probably': 0.31; 'another': 0.32; "can't": 0.32; 'skip:/ 20': 0.33; 'traceback': 0.33; 'open': 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; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'files': 0.38; 'why': 0.39; 'data': 0.39; 'sure': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'back': 0.62; 'more': 0.63; 'fall': 0.66; 'here': 0.66; '26,': 0.72; 'subject:doing': 0.84; 'abc': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8455.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100351 D'Arcy J.M. Cain wrote: > 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), A recoder converts bytes to bytes, so you have to open the file in binary mode. However, ... > 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)) ...when the recoder kicks in read_file() will return bytes which is probably not what you want. Why not just try the two encodings as in def read_file(filename): for encoding in ["utf-8", "iso-8859-1"]: try: with open(filename, encoding=encoding) as f: return f.read() except UnicodeDecodeError: pass raise AssertionError("unreachable") > > 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 > 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) >