Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'utf-8': 0.07; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; "'rb')": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'utf8': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'am,': 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; 'fri,': 0.33; 'not.': 0.33; 'subject:the': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'files': 0.38; 'rather': 0.38; 'default': 0.69; 'to:none': 0.92 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:cc :content-type:content-transfer-encoding; bh=pU7gCG9gzVAaiZe3MJgFvuoJomOCAhgizlc4+ZAV/oE=; b=uSj6DReGXGUkIAbjH1mG3xl48EgNelng+TJLbFdJDHosz+r+uOAy1IVtRFYODKXM9v ujez0VRYUSDkkCWzQdjJdV4/HPU1XttDmIBcaU0LDt6utXg9jHS1aFVAet6Tyt+Jmz8U Y+LKheVgES4lBKyYcgi2p1lFYkxqLZqrL750nMjm5t1JG5r/0g536xaEAVvytTOK7vSe IAvdmz6WpMvcIoviXbpRwXzsSv4pD2TrJhEHGAROYItO/eY5C8jAJFsRoMGU1fGBKtDG EOP8C6ndF3/kHI9J7Lm/I/R7NkW0dvbaw6ppQIQ+RNSfrXpjwbC1Yx0Gd/YTCZS11BbV h6rg== MIME-Version: 1.0 X-Received: by 10.66.129.133 with SMTP id nw5mr11615905pab.98.1389895577148; Thu, 16 Jan 2014 10:06:17 -0800 (PST) In-Reply-To: References: <52d74063$0$29970$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 17 Jan 2014 05:06:16 +1100 Subject: Re: Guessing the encoding from a BOM From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389895586 news.xs4all.nl 2844 [2001:888:2000:d::a6]:55719 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64095 On Fri, Jan 17, 2014 at 5:01 AM, Bj=C3=B6rn Lindqvist w= rote: > 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'. I'd actually rather not. It would tempt people to pollute UTF-8 files with a BOM, which is not necessary unless you are MS Notepad. ChrisA