forked from djrrb/BadgeBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvunicode.py
39 lines (30 loc) · 1.04 KB
/
csvunicode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
The default Python 2.x implementation of csv does not support unicode. Lame!
This code comes from the bottom of this document:
https://docs.python.org/2/library/csv.html
"""
import csv, codecs, cStringIO
# this is so the CSV that is read can have unicode chars. what a pain!
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
"""
A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self