1 # vim: set encoding=utf-8 et sw=4 sts=4 :
6 from cStringIO import StringIO
8 from StringIO import StringIO
11 CSV parsing and writing supporting unicode and encodings.
13 This module is copied from Python 2.5 csv module documentation:
14 http://docs.python.org/lib/csv-examples.html
16 It's adapted to work, at least, on Python 2.4.
21 Iterator that reads an encoded stream and reencodes the input to UTF-8
23 def __init__(self, f, encoding):
24 self.reader = codecs.getreader(encoding)(f)
30 return self.reader.next().encode("utf-8")
34 A CSV reader which will iterate over lines in the CSV file "f",
35 which is encoded in the given encoding.
38 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
39 f = UTF8Recoder(f, encoding)
40 self.reader = csv.reader(f, dialect=dialect, **kwds)
43 row = self.reader.next()
44 return [unicode(s, "utf-8") for s in row]
51 A CSV writer which will write rows to CSV file "f",
52 which is encoded in the given encoding.
55 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
56 # Redirect output to a queue
57 self.queue = StringIO()
58 self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
60 if hasattr(codecs, 'getincrementalencoder'):
61 self.encoder = codecs.getincrementalencoder(encoding)()
64 def __init__(self, encoding):
65 self.encoding = encoding
66 def encode(self, obj):
67 return codecs.encode(obj, encoding)
68 self.encoder = E(encoding)
70 def writerow(self, row):
71 self.writer.writerow([unicode(s).encode("utf-8") for s in row])
72 # Fetch UTF-8 output from the queue ...
73 data = self.queue.getvalue()
74 data = data.decode("utf-8")
75 # ... and reencode it into the target encoding
76 data = self.encoder.encode(data)
77 # write to the target stream
78 self.stream.write(data)
80 self.queue.truncate(0)
82 def writerows(self, rows):
86 writer = UnicodeWriter
88 reader = UnicodeReader
91 if __name__ == '__main__':
96 writer.writerows([[u"adfj", u"ñjdfhk"], [u"áalskdjal", u"1uas"]])
102 for row in reader(sio):