Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110676
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| 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 | <mailman.69.1467109546.2358.python-list@python.org> (permalink) |
| References | <ubl94dxa2e.ln2@news.c0t0d0s0.de> <nktcj3$4u8$1@ger.gmane.org> <CAPTjJmrimmk=HQmLbh1D5t-F3nq4gJh=grA7OdBZ_qynXdU6Rg@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de tMPtaUKnLsWAtlFGcdET7APS/FpF1Umqh1iNj851Y6kg== |
| Return-Path | <rosuav@gmail.com> |
| 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 | <nktcj3$4u8$1@ger.gmane.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| 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> |
| X-Mailman-Original-Message-ID | <CAPTjJmrimmk=HQmLbh1D5t-F3nq4gJh=grA7OdBZ_qynXdU6Rg@mail.gmail.com> |
| X-Mailman-Original-References | <ubl94dxa2e.ln2@news.c0t0d0s0.de> <nktcj3$4u8$1@ger.gmane.org> |
| Xref | csiph.com comp.lang.python:110676 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 09:25 +0200
Re: Processing text data with different encodings Chris Angelico <rosuav@gmail.com> - 2016-06-28 17:46 +1000
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 10:35 +0200
Re: Processing text data with different encodings Steven D'Aprano <steve@pearwood.info> - 2016-06-28 20:29 +1000
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 12:37 +0200
Re: Processing text data with different encodings Chris Angelico <rosuav@gmail.com> - 2016-06-28 21:09 +1000
Re: Processing text data with different encodings Peter Otten <__peter__@web.de> - 2016-06-28 10:30 +0200
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 12:17 +0200
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 12:44 +0200
Re: Processing text data with different encodings Steven D'Aprano <steve@pearwood.info> - 2016-06-28 21:26 +1000
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 14:30 +0200
Re: Processing text data with different encodings Steven D'Aprano <steve@pearwood.info> - 2016-06-29 00:52 +1000
Re: Processing text data with different encodings Random832 <random832@fastmail.com> - 2016-06-28 11:01 -0400
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 17:52 +0200
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-29 06:45 +0200
Re: Processing text data with different encodings Steven D'Aprano <steve@pearwood.info> - 2016-06-29 01:11 +1000
Re: Processing text data with different encodings Peter Otten <__peter__@web.de> - 2016-06-28 13:31 +0200
Re: Processing text data with different encodings Michael Welle <mwe012008@gmx.net> - 2016-06-28 15:16 +0200
Re: Processing text data with different encodings Chris Angelico <rosuav@gmail.com> - 2016-06-28 20:25 +1000
Re: Processing text data with different encodings Random832 <random832@fastmail.com> - 2016-06-28 11:52 -0400
Re: Processing text data with different encodings Chris Angelico <rosuav@gmail.com> - 2016-06-29 04:03 +1000
csiph-web