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


Groups > comp.lang.python > #64093

Re: Guessing the encoding from a BOM

References <52d74063$0$29970$c3e8da3$5496439d@news.astraweb.com>
Date 2014-01-16 19:01 +0100
Subject Re: Guessing the encoding from a BOM
From Björn Lindqvist <bjourne@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5594.1389895319.18130.python-list@python.org> (permalink)

Show all headers | View raw


2014/1/16 Steven D'Aprano <steve+comp.lang.python@pearwood.info>:
> def guess_encoding_from_bom(filename, default):
>     with open(filename, 'rb') as f:
>         sig = f.read(4)
>     if sig.startswith((b'\xFE\xFF', b'\xFF\xFE')):
>         return 'utf_16'
>     elif sig.startswith((b'\x00\x00\xFE\xFF', b'\xFF\xFE\x00\x00')):
>         return 'utf_32'
>     else:
>         return default

You might want to add the utf8 bom too: '\xEF\xBB\xBF'.

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

I like this option the most because it is the most "fail fast". If you
return 'undefined' the error might happen hours later or not at all in
some cases.


-- 
mvh/best regards Björn Lindqvist

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