Read file that starts with '\xff\xfe'

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Sep 8 09:31:49 EDT 2003


Bob Gailer <bgailer at alum.rpi.edu> wrote in
news:mailman.1063025195.15280.python-list at python.org: 

> That's a good start. I presume I need to use codecs.open(filename,
> mode[, encoding[, errors[, buffering]]]) to read the file. What is the
> actual value of the "encoding[" parameter for "Little-endian UTF-16
> Unicode character data, with CR line terminators"

Try:

 myFile = codecs.open(filename, "r", "utf16")

If the file starts with a UTF-16 marker (either little or big endian) it 
will be read correctly. If it doesn't start with either marker reading from 
it will throw a UnicodeError.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list