Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110721

Re: Processing text data with different encodings

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 Wed, 29 Jun 2016 04:03:32 +1000
Lines 51
Message-ID <mailman.91.1467137016.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> <1467129138.2539971.650945673.4DB27682@webmail.messagingengine.com> <CAPTjJmrT=Oran1+6wmBN6mTLk4yTSb_AP5ERUNa8TT_vuu_maA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de Vja+gwp+ucUrBHjm71Lg4A1Zo7FZGnzBaysDTadFBAGw==
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; 'receives': 0.03; 'subject:text': 0.04; 'encoded': 0.05; 'failing': 0.05; 'lines,': 0.05; 'character,': 0.07; 'utf-8': 0.07; 'cc:addr:python-list': 0.09; 'bytes,': 0.09; 'codecs': 0.09; 'encoding.': 0.09; 'input,': 0.09; 'likely.': 0.09; 'section,': 0.09; 'yeah,': 0.09; 'assume': 0.11; 'def': 0.13; 'wed,': 0.15; 'encoding': 0.15; '2016': 0.16; 'ascii,': 0.16; 'channel)': 0.16; 'cp1252': 0.16; 'decode': 0.16; 'encodings': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idx': 0.16; 'naive': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sufficient,': 0.16; 'vanishingly': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'bytes': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(all': 0.22; '(usually': 0.22; 'am,': 0.23; '(or': 0.23; "python's": 0.23; 'import': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'command': 0.26; 'chris': 0.26; 'error': 0.27; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'boundary': 0.29; "they'll": 0.29; '(including': 0.30; 'version,': 0.30; 'probably': 0.31; 'everyone': 0.31; "who's": 0.32; 'usually': 0.33; 'doubt': 0.33; 'tue,': 0.34; 'except': 0.34; 'server': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'text': 0.35; 'done': 0.35; 'primarily': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; 'possible': 0.36; '(i.e.': 0.36; 'subject:: ': 0.37; 'client': 0.37; 'anything': 0.38; 'several': 0.38; 'someone': 0.38; 'means': 0.39; 'whatever': 0.39; 'rather': 0.39; 'where': 0.40; 'subject:with': 0.40; 'some': 0.40; 'decision': 0.61; 'skip:u 10': 0.61; 'entire': 0.61; 'default': 0.61; 'clients': 0.61; 'different': 0.63; 'safe': 0.63; 'results': 0.66; 'situation': 0.67; 'emails,': 0.72; 'chrisa': 0.84; "op's": 0.84; "there'll": 0.84; 'to:none': 0.91; 'thing,': 0.93
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=SxaoJNwA8cZjf4zdpsZkrEti9sVrRWKPzPi+JPvxHsE=; b=YmNgQNq29X32byQOSdXltkEXgaP4jnfCRIvXVsw4VhPPgzdozIQmaS0JO161SNHDSD keV2ZHlqBo/2NQYw32IPGBNVf5bvFbk8v1kvJb/WnaP7ZtZtwEgvBOkdCzItZREvtrtV xbZfblJGG1UgzCAwQge7MyLiIz4D9LbN+V9xBWx7lp/a4DYd+rJl3bx1NsgW4M/qdYbb Vj+1JIWx2514wknJvS4TG47FBKVGpOmQq75R+UZpDNoV/GNXX8FBsvwm5DqXg3oZCk1o Eitw/lrTIjMaBGdLKWIdFfR3jnL9aoiNHHgCfMAX9i2FI2E2Lw2wgARzu7JQnX+35SRu K1wA==
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=SxaoJNwA8cZjf4zdpsZkrEti9sVrRWKPzPi+JPvxHsE=; b=G5E6HPSzxJdHccOsdPp0qxNqHrm3/gJBdJviAbLxt57+9YT9qfyhD/2L7dhnxVX4fw Q/0leuM8Gf/7zluJr3YATJev08UEMiSkvGQ6UD7wqIh3YzwVvbt2yODr40dY+7hQlaBr jfaUL8u3LCUTkc0lxlxkW1bOXFU0ZXGmcAgrfgTwu82KWsa5j64oVojG4Cv8qbWez+jn b6Dcqqn0gQfB1NsCv1VJJv6NaoL0bTE4gVt2c92z861Ih2u6yTM432cq2qgbnR3V9waH pvTszdKGTMGzpAVo7knDXgxFKxgycPIdXSdKBsr/xzHUcGDeWuju00xSNLGdO1++Tv/c SSvg==
X-Gm-Message-State ALyK8tIikb/QutD+9If8C4NcHc2le/DjjdZyTh42LMZOkiIclxNkXtpnFdHelprj5oa0eECiYKJxzjGNfQRSqg==
X-Received by 10.28.41.195 with SMTP id p186mr4958631wmp.41.1467137013465; Tue, 28 Jun 2016 11:03:33 -0700 (PDT)
In-Reply-To <1467129138.2539971.650945673.4DB27682@webmail.messagingengine.com>
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 <CAPTjJmrT=Oran1+6wmBN6mTLk4yTSb_AP5ERUNa8TT_vuu_maA@mail.gmail.com>
X-Mailman-Original-References <ubl94dxa2e.ln2@news.c0t0d0s0.de> <nktcj3$4u8$1@ger.gmane.org> <CAPTjJmrimmk=HQmLbh1D5t-F3nq4gJh=grA7OdBZ_qynXdU6Rg@mail.gmail.com> <1467129138.2539971.650945673.4DB27682@webmail.messagingengine.com>
Xref csiph.com comp.lang.python:110721

Show key headers only | View raw


On Wed, Jun 29, 2016 at 1:52 AM, Random832 <random832@fastmail.com> wrote:
> On Tue, Jun 28, 2016, at 06:25, Chris Angelico wrote:
>> 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.
>
> For completeness, this can be done character-by-character (i.e. try to
> decode a UTF-8 character, if it fails decode the offending byte as 1252)
> with an error handler:
>
> import codecs
>
> def cp1252_errors(exception):
>     input, idx = exception.object, exception.start
>     byte = input[idx:idx+1]
>     try:
>         return byte.decode('windows-1252'), idx+1
>     except UnicodeDecodeError:
>         # python's cp1252 doesn't accept 0x81, etc
>         return byte.decode('latin1'), idx+1

Yeah, and the decision as to where that boundary should be placed is
thus completely up to the application. I don't know of any situation
where you'd need the byte-by-byte version, but it's there if you want
it.

The reason I chose line-by-line in my MUD client is because of the
nature of MUDs. The server I primarily use is a naive eight-bit one -
whatever bytes it gets, it retransmits. (All the commands that it
responds to are ASCII, so we can assume that all encodings are
ASCII-compatible or the user will have major difficulties.) Some
clients (including mine) send UTF-8. If I send the command "trivia
This is a piece of text\n", the text will be encoded UTF-8, the server
receives those bytes, and then transmit (to everyone who's tuned to
the [trivia] channel) this text: "MyName [trivia] This is a piece of
text\r\n". Simplistic clients (usually on Windows) will do the same
thing, only they'll use their default encoding - usually 1252 - rather
than UTF-8. But the entire command will be encoded the same way, which
means the server will send an entire line (or several lines, if it
wraps) in the same encoding. It's safe to assume that any given line
will be in one single encoding, but consecutive lines could be in
different encodings.

For emails, it might be possible to use a larger section, but
line-by-line would be safe there too, most likely.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


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