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


Groups > comp.lang.python > #54708

Re: removing BOM prepended by codecs?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'output': 0.05; '"""': 0.07; 'encoded': 0.07; 'utf-8': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stealing': 0.09; 'variant': 0.09; 'runs': 0.10; 'python': 0.11; '2.7': 0.14; 'anyway': 0.14; '17.': 0.16; '255': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sequence:': 0.16; 'written.': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'version.': 0.19; 'written': 0.21; 'putting': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'certainly': 0.24; 'unicode': 0.24; 'looks': 0.24; "i've": 0.25; 'source': 0.25; 'header:X-Complaints-To:1': 0.27; 'characters': 0.30; "i'm": 0.30; '(which': 0.31; 'fedora': 0.31; 'overhead': 0.31; 'search.': 0.31; 'strip': 0.31; 'file': 0.32; 'probably': 0.32; 'checked': 0.32; 'open': 0.33; 'linux': 0.33; 'url:python': 0.33; 'but': 0.35; 'add': 0.35; 'doing': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'massive': 0.38; 'somebody': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'according': 0.40; 'even': 0.60; 'remove': 0.60; 'entire': 0.61; 'such': 0.63; 'our': 0.64; '(that': 0.65; 'home': 0.69; 'limit': 0.70; 'increase': 0.74; 'reliability': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: removing BOM prepended by codecs?
Date Tue, 24 Sep 2013 17:59:21 +0200
Organization None
References <5241ACFE.8070801@kent.ac.uk>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084bebe.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.299.1380038322.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380038322 news.xs4all.nl 16003 [2001:888:2000:d::a6]:50385
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54708

Show key headers only | View raw


J. Bagg wrote:

> I've checked the original files using od and they don't have BOMs.
> 
> I'll remove them in the servlet. The overhead is probably small enough
> unless somebody is doing a massive search. We have a limit anyway to
> prevent somebody stealing the entire set of data.
> 
> I started writing the Python search because the ancient C search had
> started putting out BOMs. I'm actually mystified because our home Linux
> box does not add BOMs even though it runs 2.7 but my work one does even
> though it has the same version. The only difference is Fedora 18 v
> Fedora 17.
> 
> The BOMs are certainly there:
> 
> <86> <AD><FB>%R 10C0203z-621
> %A François-Xavier Le_Bourdonnec
> 
> 0000000 206     255 373   %   R       1   0   C   0   2   0   3   z   -
> 
> J
> 

Were these files edited with Notepad? According to

http://docs.python.org/2/library/codecs.html#encodings-and-unicode

"""
To increase the reliability with which a UTF-8 encoding can be detected, 
Microsoft invented a variant of UTF-8 (that Python 2.5 calls "utf-8-sig") 
for its Notepad program: Before any of the Unicode characters is written to 
the file, a UTF-8 encoded BOM (which looks like this as a byte sequence: 
0xef, 0xbb, 0xbf) is written.
"""

To strip off such a UTF-8 encoded BOM you can open the source file with 
"utf-8-sig" and write the output to a (different!) file with "utf-8"

with codecs.open(source, "r", encoding="utf-8-sig") as instream:
    with codecs.open(dest, "w", encoding="utf-8") as outstream:
        shutil.copyfileobj(instream, outstream)

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


Thread

Re: removing BOM prepended by codecs? Peter Otten <__peter__@web.de> - 2013-09-24 17:59 +0200

csiph-web