Package jazzparser :: Package utils :: Module csv
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.utils.csv

 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   
16 -class UTF8Recoder(object):
17 """ 18 Iterator that reads an encoded stream and reencodes the input to 19 UTF-8. 20 21 """
22 - def __init__(self, f, encoding):
23 import codecs 24 self.reader = codecs.getreader(encoding)(f)
25
26 - def __iter__(self):
27 return self
28
29 - def next(self):
30 return self.reader.next().encode("utf-8")
31
32 -class UnicodeCsvReader(object):
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):
39 import csv 40 f = UTF8Recoder(f, encoding) 41 self.reader = csv.reader(f, dialect=dialect, **kwds)
42
43 - def next(self):
44 row = self.reader.next() 45 return [unicode(s, "utf-8") for s in row]
46
47 - def __iter__(self):
48 return self
49
50 -class UnicodeCsvWriter(object):
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 # Redirect output to a queue 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
64 - def writerow(self, row):
65 self.writer.writerow([unicode(s).encode("utf-8") for s in row]) 66 # Fetch UTF-8 output from the queue ... 67 data = self.queue.getvalue() 68 data = data.decode("utf-8") 69 # ... and reencode it into the target encoding 70 data = self.encoder.encode(data) 71 # write to the target stream 72 self.stream.write(data) 73 # empty queue 74 self.queue.truncate(0)
75
76 - def writerows(self, rows):
77 for row in rows: 78 self.writerow(row)
79