print UTF-8 file with BOM

davihigh at gmail.com davihigh at gmail.com
Thu Dec 22 21:12:28 EST 2005


Hi Friends:

	fileObj = codecs.open( filename, "r", "utf-8" )
	u = fileObj.read() # Returns a Unicode string from the UTF-8 bytes in
the file
	print u

It says error:
	UnicodeEncodeError: 'gbk' codec can't encode character u'\ufeff' in
position 0:
	illegal multibyte sequence

I want to know how read from UTF-8 file, and convert to specified
locale (default is current system locale) and print out string. I hope
put away BOM header automatically.

Rgds, David




More information about the Python-list mailing list