Exports the table to a unicode text file at the given path. Rows in the file are separated with a newline. Columns in a row are separated with the given separator (by default, comma). For data types other than string, int, float, bool or None, a custom string enc
(self, path, separator=",", encoder=lambda v: v, headers=False, **kwargs)
| 1572 | headers = property(_get_headers, _set_headers) |
| 1573 | |
| 1574 | def save(self, path, separator=",", encoder=lambda v: v, headers=False, **kwargs): |
| 1575 | """ Exports the table to a unicode text file at the given path. |
| 1576 | Rows in the file are separated with a newline. |
| 1577 | Columns in a row are separated with the given separator (by default, comma). |
| 1578 | For data types other than string, int, float, bool or None, a custom string encoder can be given. |
| 1579 | """ |
| 1580 | # Optional parameters include all arguments for csv.writer(), see: |
| 1581 | # http://docs.python.org/library/csv.html#csv.writer |
| 1582 | kwargs.setdefault("delimiter", separator) |
| 1583 | kwargs.setdefault("quoting", csv.QUOTE_ALL) |
| 1584 | # csv.writer will handle str, int, float and bool: |
| 1585 | s = StringIO() |
| 1586 | w = csv.writer(s, **kwargs) |
| 1587 | if headers and self.fields is not None: |
| 1588 | w.writerows([[csv_header_encode(name, type) for name, type in self.fields]]) |
| 1589 | w.writerows([[encode_utf8(encoder(v)) for v in row] for row in self]) |
| 1590 | s = s.getvalue() |
| 1591 | s = s.strip() |
| 1592 | s = re.sub("([^\"]|^)\"None\"", "\\1None", s) |
| 1593 | f = open(path, "wb") |
| 1594 | f.write(BOM_UTF8) |
| 1595 | f.write(s) |
| 1596 | f.close() |
| 1597 | |
| 1598 | @classmethod |
| 1599 | def load(cls, path, separator=",", decoder=lambda v: v, headers=False, preprocess=lambda s: s): |