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


Groups > comp.lang.python > #64046

Re: Guessing the encoding from a BOM

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!newsfeed.freenet.ag!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'encoding': 0.05; 'class,': 0.07; 'explicit': 0.07; 'none,': 0.07; 'result,': 0.07; 'exception.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; "'unknown',": 0.16; '+1.': 0.16; 'enc': 0.16; 'failure.': 0.16; 'finney': 0.16; 'guess.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'exception': 0.16; '(but': 0.19; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'returned': 0.30; 'skip:g 30': 0.30; "i'm": 0.30; 'agreed.': 0.31; "d'aprano": 0.31; 'helpful.': 0.31; 'name;': 0.31; 'steven': 0.31; 'writes:': 0.31; 'run': 0.32; 'subject:the': 0.34; "i'd": 0.34; 'subject:from': 0.34; "can't": 0.35; '(2)': 0.35; 'but': 0.35; 'there': 0.35; 'returning': 0.36; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; '(3)': 0.38; 'ben': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'results.': 0.60; 'new': 0.61; 'back': 0.62; 'name': 0.63; 'real': 0.63; 'soon': 0.63; 'strategy': 0.64; 'love': 0.65; 'default': 0.69; 'guaranteed': 0.75; 'describes': 0.84; 'silently': 0.84; 'yelling': 0.84; 'philips': 0.91; 'outcome': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Guessing the encoding from a BOM
Date Thu, 16 Jan 2014 14:47:00 +1100
References <52d74063$0$29970$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host rasputin.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:XhBQurtgcqbbQyllYMi2/amyuWg=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.5566.1389844041.18130.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389844041 news.xs4all.nl 2857 [2001:888:2000:d::a6]:49719
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64046

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> enc = guess_encoding_from_bom("filename")
> if enc == something:
>      # Can't guess, fall back on an alternative strategy
>      ...
> else:
>      f = open("filename", encoding=enc)
>
>
> If I forget to check the returned result, I should get an explicit
> failure as soon as I try to use it, rather than silently returning the
> wrong results.

Yes, agreed.

> What should I return as the default default? I have four possibilities:
>
>     (1) 'undefined', which is an standard encoding guaranteed to 
>         raise an exception when used;

+0.5. This describes the outcome of the guess.

>     (2) 'unknown', which best describes the result, and currently 
>         there is no encoding with that name;

+0. This *better* describes the outcome, but I don't think adding a new
name is needed nor very helpful.

>     (3) None, which is not the name of an encoding; or

−1. This is too much like a real result and doesn't adequately indicate
the failure.

>     (4) Don't return anything, but raise an exception. (But 
>         which exception?)

+1. I'd like a custom exception class, sub-classed from ValueError.

-- 
 \       “I love to go down to the schoolyard and watch all the little |
  `\   children jump up and down and run around yelling and screaming. |
_o__)             They don't know I'm only using blanks.” —Emo Philips |
Ben Finney

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


Thread

Guessing the encoding from a BOM Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-16 02:13 +0000
  Re: Guessing the encoding from a BOM Ben Finney <ben+python@benfinney.id.au> - 2014-01-16 14:47 +1100
    Re: Guessing the encoding from a BOM Steven D'Aprano <steve@pearwood.info> - 2014-01-16 06:55 +0000
      Re: Guessing the encoding from a BOM Ethan Furman <ethan@stoneleaf.us> - 2014-01-15 23:29 -0800
  Re: Guessing the encoding from a BOM Chris Angelico <rosuav@gmail.com> - 2014-01-16 16:01 +1100
    Re: Guessing the encoding from a BOM Steven D'Aprano <steve@pearwood.info> - 2014-01-16 06:45 +0000
  Re: Guessing the encoding from a BOM Ethan Furman <ethan@stoneleaf.us> - 2014-01-15 21:40 -0800
  Re: Guessing the encoding from a BOM Björn Lindqvist <bjourne@gmail.com> - 2014-01-16 19:01 +0100
  Re: Guessing the encoding from a BOM Chris Angelico <rosuav@gmail.com> - 2014-01-17 05:06 +1100
  Re: Guessing the encoding from a BOM Tim Chase <python.list@tim.thechases.com> - 2014-01-16 12:50 -0600

csiph-web