writing \feff at the begining of a file
MRAB
python at mrabarnett.plus.com
Fri Aug 13 13:45:22 EDT 2010
Jean-Michel Pichavant wrote:
> Hello python world,
>
> I'm trying to update the content of a $Microsoft$ VC2005 project files
> using a python application.
> Since those files are XML data, I assumed I could easily do that.
>
> My problem is that VC somehow thinks that the file is corrupted and
> update the file like the following:
>
> -<?xml version='1.0' encoding='UTF-8'?>
> +?<feff><?xml version="1.0" encoding="UTF-8"?>
>
>
> Actually, <feff> is displayed in a different color by vim, telling me
> that this is some kind of special caracter code (I'm no familiar with
> such thing).
> After googling that, I have a clue : could be some unicode caracter use
> to indicate something ... well I don't know in fact ("UTF-8 files
> sometimes start with a byte-order marker (BOM) to indicate that they are
> encoded in UTF-8.").
>
> My problem is however simplier : how do I add such character at the
> begining of the file ?
> I tried
>
> f = open('paf', w)
> f.write(u'\ufeff')
>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in
> position 0: ordinal not in range(128)
>
> The error may be explicit but I have no idea how to proceed further. Any
> clue ?
>
In Python 2 the default encoding is 'ascii'. What you want is 'utf-8'.
Use codecs.open() instead, with the 'utf-8-sig' encoding, which will
include the BOM.
More information about the Python-list
mailing list