Adding BOM (unicode signature) while saving file in python

Why do you think you need to specifically make it UTF16LE? Just use 'utf16' as the encoding, Python will write it in your endianness with the appropriate BOM, and all the consumer needs to be told is that the file is UTF-16 ... that's the whole point of having a BOM.

If the consumer is insisting that the file must be encoded in UTF16LE, then you don't need a BOM.

If the file is written the way that you specify, and the consumer opens it with UTF16LE encoding, they will get a \ufeff at the start of the file, which is a nuisance, and needs to be ignored.


Write it directly at the beginning of the file:

file_new.write('\ufeff')

Just choose the encoding with BOM:

with codecs.open('outputfile.csv', 'w', 'utf-8-sig') as f:
   f.write('a,é')

(In python 3 you can drop the codecs.)


It's better to use constants from 'codecs' module.

import codecs
f.write(codecs.BOM_UTF16_LE)

Tags:

Python