Skip to content

Commit 64294fb

Browse files
authored
Merge pull request draperjames#47 from mherrmann/master
Fix UnicodeError in superReadCSV(...)
2 parents b6d501a + 9f1399a commit 64294fb

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

qtpandas/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ def superReadCSV(filepath, first_codec='UTF_8', usecols=None,
7171

7272
assert isinstance(first_codec, str), "first_codec must be a string"
7373

74-
codecs = list(set([first_codec] + ['UTF_8', 'ISO-8859-1', 'ASCII',
75-
'UTF_16', 'UTF_32']))
74+
codecs = ['UTF_8', 'ISO-8859-1', 'ASCII', 'UTF_16', 'UTF_32']
75+
try:
76+
codecs.remove(first_codec)
77+
except ValueError as not_in_list:
78+
pass
79+
codecs.insert(0, first_codec)
7680
errors = []
7781
for c in codecs:
7882
try:
@@ -86,8 +90,10 @@ def superReadCSV(filepath, first_codec='UTF_8', usecols=None,
8690
sep=sep,
8791
chunksize=chunksize,
8892
**kwargs)
89-
90-
except (UnicodeDecodeError, UnboundLocalError) as e:
93+
# Need to catch `UnicodeError` here, not just `UnicodeDecodeError`,
94+
# because pandas 0.23.1 raises it when decoding with UTF_16 and the
95+
# file is not in that format:
96+
except (UnicodeError, UnboundLocalError) as e:
9197
errors.append(e)
9298
except Exception as e:
9399
errors.append(e)

0 commit comments

Comments
 (0)