Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'exception.': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "'rb')": 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'utf8': 0.16; '(but': 0.19; 'later': 0.20; 'cc:addr:python.org': 0.22; 'error': 0.23; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'skip:g 30': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; "d'aprano": 0.31; 'steven': 0.31; 'option': 0.32; 'subject:the': 0.34; 'subject:from': 0.34; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'most': 0.60; 'happen': 0.63; 'hours': 0.66; 'default': 0.69 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=/Uw85arRyMh6JmhhCsBeg+/aj02blG4lhOQLF510g8I=; b=MXaSc7/ApcOP3tsfEUVbbSpSorhfp5VfQ7dVeFr5K8Kqd2cE9onTwcCknlj1Tbda5K Ad4+NtTsFKFlIXLHqNpEShl8HhH1yhfmf23sOeFdU1CLaCshygeCwzlI2XjR86bkJw3p qYkcBg3t4OaKWe6gMkQAJk3AVJ/rSFtOH+KKRzzksRJAW+azU6FQCzrsiiA1SXxzpiTI UIN8m1ErHOELn+9jA5HUBEFXAswwVaEV5/oznGmY1wnn1uH+0uL1IAijUH9Xpi3sPT5j 1YVUU5dvIlULTEgH4I9TrB8HLhsYAH8IC8BusJZYStdlEmjcnPLoFR+6EslfpIC3mgE3 6s6Q== MIME-Version: 1.0 X-Received: by 10.140.20.17 with SMTP id 17mr10489039qgi.28.1389895311154; Thu, 16 Jan 2014 10:01:51 -0800 (PST) In-Reply-To: <52d74063$0$29970$c3e8da3$5496439d@news.astraweb.com> References: <52d74063$0$29970$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 16 Jan 2014 19:01:51 +0100 Subject: Re: Guessing the encoding from a BOM From: =?ISO-8859-1?Q?Bj=F6rn_Lindqvist?= To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389895319 news.xs4all.nl 2882 [2001:888:2000:d::a6]:51037 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64093 2014/1/16 Steven D'Aprano : > def guess_encoding_from_bom(filename, default): > with open(filename, 'rb') as f: > sig =3D 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. --=20 mvh/best regards Bj=F6rn Lindqvist