case for if no BOM found & autodetect_encoding=False
@@ -131,6 +131,23 @@ def open_with_codecs(filename, encoding, encoding_errors,
encoding = 'utf-8-sig'
autodetect_encoding = False
+ # And if no BOM & autodetect_encoding=False
+ if (not encoding) and (not autodetect_encoding):
+ test_encodings = ['windows-1252', 'latin-1', 'ascii']
+ for i in test_encodings:
+ encoding = i
+ with open(filename, mode='r', encoding=encoding) as f:
+ try:
+ if nbytes:
+ f.read(nbytes)
+ else:
+ f.readline()
+ break
+ except UnicodeDecodeError:
+ logger.debug('{} tested, raised UnicodeDecodeError'.format(encoding))
+ pass
+ encoding = None
+
# Otherwise...
if autodetect_encoding:
with open(filename, mode='rb') as test:
@@ -457,4 +474,3 @@ def read_line(line, pattern=None):
if d[key].endswith('.'):
d[key] = d[key].strip('.') # see issue #36
return d
-
