Skip to content

Instantly share code, notes, and snippets.

@godmar
Created February 1, 2016 19:20
Show Gist options
  • Save godmar/891749b267ab74d3268f to your computer and use it in GitHub Desktop.
Save godmar/891749b267ab74d3268f to your computer and use it in GitHub Desktop.
falling back to ISO 8859
diff --git a/pycounter/csvhelper.py b/pycounter/csvhelper.py
index d77c645..ef7f850 100644
--- a/pycounter/csvhelper.py
+++ b/pycounter/csvhelper.py
@@ -24,7 +24,7 @@ class UnicodeReader(six.Iterator):
self.fileobj = open(self.filename, 'rt',
encoding=self.encoding, newline='')
else:
- self.fileobj = open(self.filename, 'rb')
+ self.fileobj = open(self.filename, 'rU')
self.reader = csv.reader(self.fileobj, dialect=self.dialect,
**self.kwargs)
return self
@@ -36,7 +36,12 @@ class UnicodeReader(six.Iterator):
row = next(self.reader)
if six.PY3:
return row
- return [s.decode("utf-8") for s in row]
+ try:
+ return [s.decode("utf-8") for s in row]
+ except UnicodeDecodeError:
+ # fall back to ISO 8859-1 if that fails
+ # but Python 3 will not get here.
+ return [s.decode("iso8859") for s in row]
def __iter__(self):
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment