Created
March 21, 2016 08:05
-
-
Save buptxge/15874e27e612a7a2a8a2 to your computer and use it in GitHub Desktop.
Deal with utf8 bom encoding problem. from http://stackoverflow.com/questions/13590749/reading-unicode-file-data-with-bom-chars-in-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
import chardet | |
bytes = min(32, os.path.getsize(filename)) | |
raw = open(filename, 'rb').read(bytes) | |
if raw.startswith(codecs.BOM_UTF8): | |
encoding = 'utf-8-sig' | |
else: | |
result = chardet.detect(raw) | |
encoding = result['encoding'] | |
infile = codecs.open(filename, mode, encoding=encoding) | |
data = infile.read() | |
infile.close() | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment