Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110667
| 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 17:46:09 +1000 |
| Lines | 49 |
| Message-ID | <mailman.67.1467099972.2358.python-list@python.org> (permalink) |
| References | <ubl94dxa2e.ln2@news.c0t0d0s0.de> <CAPTjJmqRXOkTjBxwO=vR29gFQfmg-nmBvgeTFucAAjMUWHnq2Q@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de S/oSFAD4+Wro63150FLWvwh8cc3yjvg+5IcxzmQ542bw== |
| Return-Path | <rosuav@gmail.com> |
| 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; 'anyway.': 0.04; 'string.': 0.04; 'subject:text': 0.04; 'correct.': 0.07; 'line:': 0.07; 'utf-8': 0.07; 'cc:addr:python-list': 0.09; 'encode': 0.09; 'tackle': 0.09; 'text"': 0.09; 'python': 0.10; '(but': 0.15; 'encoding': 0.15; '(assuming': 0.16; '2016': 0.16; 'ascii,': 0.16; 'codec': 0.16; 'decode': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ideally,': 0.16; 'input:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'detect': 0.18; "shouldn't": 0.18; 'try:': 0.18; 'input': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'algorithm': 0.20; 'not,': 0.22; 'ascii': 0.22; 'simpler': 0.22; 'text,': 0.22; 'this:': 0.23; 'import': 0.24; '(this': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'module': 0.25; 'logging': 0.27; 'message-id:@mail.gmail.com': 0.27; 'data,': 0.27; 'yield': 0.27; 'idea': 0.28; 'looks': 0.29; 'character.': 0.29; 'code': 0.30; 'certainly': 0.30; "can't": 0.32; 'maybe': 0.33; 'problem': 0.33; 'michael': 0.33; 'similar': 0.33; 'correctly': 0.34; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'next': 0.35; 'could': 0.35; 'text': 0.35; 'unicode': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'data': 0.39; 'where': 0.40; 'subject:with': 0.40; 'some': 0.40; 'save': 0.60; 'skip:u 10': 0.61; 'more': 0.63; 'different': 0.63; 'times': 0.63; 'yourself': 0.73; "(don't": 0.84; 'chrisa': 0.84; 'welle': 0.84; 'to:none': 0.91; 'trouble.': 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=zUoP6ayJBhWRgb/uZFy1Xcke0SIulSk/6Pa6+fo+l30=; b=w96Fmw1xkibOPD6RCPPO8U6JuZ5di067tL2VSjUpdSUZdZiz5MvugtKTxCif5pYkS6 K/G/rwwyGNhXMqiDoC3hZnOEaSX0X319jyPHod+KHDOyRPIH+Jo33nNVMhdc8tC1Chx8 7IcgsSFsAPGTc34fTXk5q2tbvmKop4A3YcxluewrOtnTPAJO6Kmi6hwZYBDWjrfrDRXR EXZqBD2CU1VLGxNzOgTfDr326gXL7PkAE2hq3anXMQ7o68sXRYR8Iw3c9/JU0p/Xf0xc SUP/A59PhSqsHvQxUsqfUhqXlNT4tpMvdw+6N5SRjCxg1KXD/9feAHJeFNk5oL6a3K3r LpSQ== |
| 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=zUoP6ayJBhWRgb/uZFy1Xcke0SIulSk/6Pa6+fo+l30=; b=kbhEOQdHWxu8LKx113RcyhXgBSlzDLksRHDBcQKf7IArCwxiBuKMNeRbZVRxtXBL0h zX85j5/FDsqO2wysDblADjhQavbrgzV+nFKchSU6X+5qjnIdFU3DWZ7U/072t6M0h0EX fqJ8DpqvjVsS+9eHJGPdZG7Wr8hlphsLrgl6UmD8x+25OdEhKJ3yxlNb05YDu30r1MdY RxINXCrvuE93JSDCaDGWuZ2bc9iM+FjSYV2AQQlOhcRALCNrOhMRG2ZDxjUq3n0HHeVW OisCp/HhG3rYeeoA2xLTP8nUcsLfbPMHSg7H0+0txHY1V9hx76a83APaR3NsuePBkvDX jebw== |
| X-Gm-Message-State | ALyK8tLjozqQTezdV290nrj6f3TloEWkd2nqwLx+c1ReYGvyzCkQJ1z8uGk8059+pLA05lWBxZSFgpe8Qa8vkw== |
| X-Received | by 10.28.223.215 with SMTP id w206mr14017502wmg.61.1467099969957; Tue, 28 Jun 2016 00:46:09 -0700 (PDT) |
| In-Reply-To | <ubl94dxa2e.ln2@news.c0t0d0s0.de> |
| 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 | <CAPTjJmqRXOkTjBxwO=vR29gFQfmg-nmBvgeTFucAAjMUWHnq2Q@mail.gmail.com> |
| X-Mailman-Original-References | <ubl94dxa2e.ln2@news.c0t0d0s0.de> |
| Xref | csiph.com comp.lang.python:110667 |
Show key headers only | View raw
On Tue, Jun 28, 2016 at 5:25 PM, Michael Welle <mwe012008@gmx.net> wrote:
> I want to use Python 3 to process data, that unfortunately come with
> different encodings. So far I have found ascii, iso-8859, utf-8,
> windows-1252 and maybe some more in the same file (don't ask...). I read
> the data via sys.stdin and the idea is to read a line, detect the
> current encoding, hit it until it looks like utf-8 and then go on with
> the next line of input:
>
>
> import cchardet
>
> for line in sys.stdin.buffer:
>
> encoding = cchardet.detect(line)['encoding']
> line = line.decode(encoding, 'ignore')\
> .encode('UTF-8').decode('UTF-8', 'ignore')
>
>
> After that line should be a string. The logging module and some others
> choke on line: UnicodeEncodeError: 'charmap' codec can't encode
> character. What would be a right approach to tackle that problem
> (assuming that I can't change the input data)?
This is the exact sort of "ewwww" that I have to cope with in my MUD
client. Sometimes it gets sent UTF-8, other times it gets sent...
uhhhh... some eight-bit encoding, most likely either 8859 or 1252 (but
could theoretically be anything). The way I cope with it is to do a
line-by-line decode, similar to what you're doing, but with a much
simpler algorithm - something like this:
for line in <binary source>:
try:
line = line.decode("UTF-8")
except UnicodeDecodeError:
line = line.decode("1252")
yield line
There's no need to chardet for UTF-8; if you successfully decode the
text, it's almost certainly correct. (This includes pure ASCII text,
which would also decode successfully and correctly as ISO-8859 or
Windows-1252.)
You shouldn't need this complicated triple-encode dance. Just decode
it once and work with text from there on. Ideally, you should be using
Python 3, where "work[ing] with text" is exactly how most of the code
wants to work; if not, resign yourself to reprs with u-prefixes, and
work with Unicode strings anyway. It'll save you a lot of trouble.
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