This is the difference between UTF-16LE
and UTF-16
UTF-16LE
is little endian without a BOM
UTF-16
is big or little endian with a BOM
So when you use UTF-16LE
, the BOM is just part of the text. Use UTF-16
instead, so the BOM is automatically removed. The reason UTF-16LE
and UTF-16BE
exist is so people can carry around "properly-encoded" text without BOMs, which does not apply to you.
Note what happens when you encode using one encoding and decode using the other. (UTF-16
automatically detects UTF-16LE
sometimes, not always.)
>>> u'Hello, world'.encode('UTF-16LE')
'Hx00ex00lx00lx00ox00,x00 x00wx00ox00rx00lx00dx00'
>>> u'Hello, world'.encode('UTF-16')
'xffxfeHx00ex00lx00lx00ox00,x00 x00wx00ox00rx00lx00dx00'
^^^^^^^^ (BOM)
>>> u'Hello, world'.encode('UTF-16LE').decode('UTF-16')
u'Hello, world'
>>> u'Hello, world'.encode('UTF-16').decode('UTF-16LE')
u'ufeffHello, world'
^^^^ (BOM)
Or you can do this at the shell:
for x in * ; do iconv -f UTF-16 -t UTF-8 <"$x" | dos2unix >"$x.tmp" && mv "$x.tmp" "$x"; done
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…