Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Processing text data with different encodings Date: Tue, 28 Jun 2016 20:25:44 +1000 Lines: 29 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de tMPtaUKnLsWAtlFGcdET7APS/FpF1Umqh1iNj851Y6kg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'handler': 0.04; 'subject:text': 0.04; 'encoded': 0.05; 'failing': 0.05; 'cc:addr :python-list': 0.09; 'decodes': 0.09; 'wrong,': 0.09; 'encoding': 0.15; 'file,': 0.15; '2016': 0.16; '6:30': 0.16; 'decode': 0.16; 'detects': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'magic': 0.16; 'pipes': 0.16; 'python),': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sufficient,': 0.16; 'vanishingly': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'windows': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'assuming': 0.22; 'parameter': 0.22; 'second': 0.24; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'command': 0.26; '(which': 0.26; 'figure': 0.27; 'error': 0.27; 'not.': 0.27; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'values': 0.28; 'boundary': 0.29; 'handful': 0.29; 'becomes': 0.30; 'probably': 0.31; "can't": 0.32; 'usually': 0.33; 'doubt': 0.33; 'correctly': 0.34; 'tue,': 0.34; 'that,': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'so,': 0.35; 'something': 0.35; 'possible': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'anything': 0.38; 'someone': 0.38; 'files': 0.38; 'end': 0.39; 'does': 0.39; 'subject:with': 0.40; 'ever': 0.60; 'yes': 0.62; 'great': 0.63; 'results': 0.66; 'obvious': 0.76; 'chrisa': 0.84; "op's": 0.84; 'otten': 0.84; "there'll": 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=H8ir/viqIw/d/akRhEWrz+Qv16u9API6s+m1c6gMXy0=; b=NGz4yFNL5BFUt6EZspqgyEeetjo8pZ94Vf2vEhSZIm+O5Yl4+d4rxpDT4aKbGMheHw BGU/tJ3qF/vxDqnPjWNP4PEOvrcTiO4HTogUIQJlYhGy+QlDD4mSI6Xyb2BnO6BRFxmk ywf9NTy2zNC9GcXuSeyLhClPPNFy5LKVXdZ3nWnP6F+e352rJhBEZ/9UOhTQmgu7LVmA 3ESjR/KJdH/ByhFKjBDMayzOwf5lkYbZXIV8kQDSRyKdl3KkYUqHWq+2ReBeRjwXK/F6 RmiILr/edyDBrzZTY+pqBfQ6esShiDjpOJg83FE43J8LC3i2vkPhAX0/qbr2PXSBIA/u 69zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=H8ir/viqIw/d/akRhEWrz+Qv16u9API6s+m1c6gMXy0=; b=Y16yd2fDFwJXK7+9m2Lu1UUqekDZv4fOH6fooARQqYfMlVcaYUqPEkQBaUNRL1Bcu/ SPWAz/QNVAlBPUApX3g+uIgj0gGk5hlDn8t5rngIckwzWKypeanYQs5qGzmtXoz7nv/+ g+mZ8iy0dl4/pi1tIr/Dvm45kHVTW8WZPckrZqxL4f5xMEBbFgxBFnA/R2ZCU4uODu0M AqNJ7/ohh0L/S/tlYnMke1DmSt4Xw72rMqju+SIGkg7LRmuPO2OwjvHCCjMg7yWlDwBO tsEpub/ofcqYYLbBGDO7YQ1u8cLOlTnO3M5FhdQFKBgVRCzD50ANqLlwntKbvWZ5N6lj wfEg== X-Gm-Message-State: ALyK8tIWobHfzDM7HlwoVUoEWwSOcoVYW6KJt7oVdiBa3n8za+sRoR5PJMLKjuAGZilqJ5qQRCNqyORQJQ0Q2w== X-Received: by 10.194.97.201 with SMTP id ec9mr2220925wjb.117.1467109545265; Tue, 28 Jun 2016 03:25:45 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:110676 On Tue, Jun 28, 2016 at 6:30 PM, Peter Otten <__peter__@web.de> wrote: > Does chardet ever return an encoding that fails to decode > the line? Only in that case the "ignore" error handler would make sense. Assuming the module the OP is using is functionally identical to the one I use from the command line (which is implemented in Python), yes it can. Usually what happens is that it detects something as an ISO-8859-* when it's actually the corresponding Windows codepage; if you try to decode it that way, you end up with a handful of byte values that don't correctly decode. I have a "cdless" command that does a chardet, decodes the file, re-encodes as UTF-8, and pipes the result into less(1); great way to figure out what encoding something is (if it gets it wrong, it's usually really obvious to a human). It has a magic second parameter "win" to switch from ISO-8859 to Windows encoding - ISO-8859-1 becomes Windows-1252, -2 becomes 1250, etc. Additionally, chardet often returns "MacCyrillic" for files that are actually encoded Windows-1256 (Arabic). So, yes, it's definitely possible for chardet to pick something that you can't actually decode with. For the OP's situation, frankly, I doubt there'll be anything other than UTF-8, Latin-1, and CP-1252. The chances that someone casually mixes CP-1252 with (say) CP-1254 would be vanishingly small. So the simple decode of "UTF-8, or failing that, 1252" is probably going to give correct results for most of the content. The trick is figuring out a correct boundary for the check; line-by-line may be sufficient, or it may not. ChrisA