1 from __future__ import absolute_import
2 """Additional CSV reading/writing utilities.
3
4 Comma-separated value file reader and writer that wraps the standard
5 package to handle unicode.
6
7 This stuff comes straight from the example in the CSV package's
8 documentation.
9
10 Original author: Skip Montanaro <skip@pobox.com>
11
12 @see: U{http://docs.python.org/library/csv.html}
13
14 """
15
17 """
18 Iterator that reads an encoded stream and reencodes the input to
19 UTF-8.
20
21 """
23 import codecs
24 self.reader = codecs.getreader(encoding)(f)
25
28
31
33 """
34 A CSV reader which will iterate over lines in the CSV file "f",
35 which is encoded in the given encoding.
36
37 """
38 - def __init__(self, f, dialect='excel', encoding="utf-8", **kwds):
42
44 row = self.reader.next()
45 return [unicode(s, "utf-8") for s in row]
46
49
51 """
52 A CSV writer which will write rows to CSV file "f",
53 which is encoded in the given encoding.
54
55 """
56 - def __init__(self, f, dialect='excel', encoding="utf-8", **kwds):
57 import csv, cStringIO, codecs
58
59 self.queue = cStringIO.StringIO()
60 self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
61 self.stream = f
62 self.encoder = codecs.getincrementalencoder(encoding)()
63
75
77 for row in rows:
78 self.writerow(row)
79