|
@ -29,18 +29,18 @@ def detect_encoding(filename): |
|
|
"""Return the most likely encoding of the file |
|
|
"""Return the most likely encoding of the file |
|
|
""" |
|
|
""" |
|
|
encodings = ['utf-8', 'windows-1250', 'windows-1252'] |
|
|
encodings = ['utf-8', 'windows-1250', 'windows-1252'] |
|
|
for e in encodings: |
|
|
for encoding in encodings: |
|
|
try: |
|
|
try: |
|
|
fh = codecs.open(filename, 'r', encoding=e) |
|
|
filehandler = codecs.open(filename, 'r', encoding=encoding) |
|
|
fh.readlines() |
|
|
filehandler.readlines() |
|
|
fh.seek(0) |
|
|
filehandler.seek(0) |
|
|
except UnicodeDecodeError: |
|
|
except UnicodeDecodeError: |
|
|
pass |
|
|
pass |
|
|
else: |
|
|
else: |
|
|
if e != 'utf-8': |
|
|
if encoding != 'utf-8': |
|
|
print('Opening `%s` with `%s` encoding' % (filename, e)) |
|
|
print('Opening `%s` with `%s` encoding' % (filename, encoding)) |
|
|
return e |
|
|
return encoding |
|
|
finally: |
|
|
finally: |
|
|
fh.close() |
|
|
filehandler.close() |
|
|
|
|
|
|
|
|
raise Exception('Not suitable encoding found for {}'.format(filename)) |
|
|
raise Exception('Not suitable encoding found for {}'.format(filename)) |
|
|