MCPcopy
hub / github.com/catherinedevlin/ipython-sql / UnicodeWriter

Class UnicodeWriter

src/sql/run.py:27–63  ·  view source on GitHub ↗

A CSV writer which will write rows to CSV file "f", which is encoded in the given encoding.

Source from the content-addressed store, hash-verified

25 return res
26
27class UnicodeWriter(object):
28 """
29 A CSV writer which will write rows to CSV file "f",
30 which is encoded in the given encoding.
31 """
32
33 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
34 # Redirect output to a queue
35 self.queue = six.StringIO()
36 self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
37 self.stream = f
38 self.encoder = codecs.getincrementalencoder(encoding)()
39
40 def writerow(self, row):
41 if six.PY2:
42 _row = [s.encode("utf-8")
43 if hasattr(s, "encode")
44 else s
45 for s in row]
46 else:
47 _row = row
48 self.writer.writerow(_row)
49 # Fetch UTF-8 output from the queue ...
50 data = self.queue.getvalue()
51 if six.PY2:
52 data = data.decode("utf-8")
53 # ... and reencode it into the target encoding
54 data = self.encoder.encode(data)
55 # write to the target stream
56 self.stream.write(data)
57 # empty queue
58 self.queue.truncate(0)
59 self.queue.seek(0)
60
61 def writerows(self, rows):
62 for row in rows:
63 self.writerow(row)
64
65class CsvResultDescriptor(object):
66 """Provides IPython Notebook-friendly output for the feedback after a ``.csv`` called."""

Callers 1

csvMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected