summaryrefslogtreecommitdiff
path: root/tablib
diff options
context:
space:
mode:
authorLuca Beltrame <einar@heavensinferno.net>2010-10-19 10:45:54 +0200
committerLuca Beltrame <einar@heavensinferno.net>2010-10-19 10:45:54 +0200
commite75a00541d83c1df6ede20fec3a5611f960b6821 (patch)
tree9c98f06fe78befc84cc0b73a58239cd28daeb8c0 /tablib
parent3b0e0c7991da92e8a65c637afd19ac52e93deddd (diff)
downloadtablib-e75a00541d83c1df6ede20fec3a5611f960b6821.tar.gz
Support for TSV-files. Unit-tested.
Diffstat (limited to 'tablib')
-rw-r--r--tablib/core.py14
-rw-r--r--tablib/formats/__init__.py3
-rw-r--r--tablib/formats/_tsv.py53
3 files changed, 69 insertions, 1 deletions
diff --git a/tablib/core.py b/tablib/core.py
index 6e5e60b..c4071c8 100644
--- a/tablib/core.py
+++ b/tablib/core.py
@@ -307,6 +307,20 @@ class Dataset(object):
pass
@property
+ def tsv():
+ """A TSV representation of the :class:`Dataset` object. The top row will contain
+ headers, if they have been set. Otherwise, the top row will contain
+ the first row of the dataset.
+
+ A dataset object can also be imported by setting the :class:`Dataset.csv` attribute. ::
+
+ data = tablib.Dataset()
+ data.tsv = 'age\tfirst_name\tlast_name\\n90\tJohn\tAdams'
+
+ Import assumes (for now) that headers exist.
+ """
+
+ @property
def yaml():
"""A YAML representation of the :class:`Dataset` object. If headers have been
set, a YAML list of objects will be returned. If no headers have
diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py
index 0ce9b71..f5960b8 100644
--- a/tablib/formats/__init__.py
+++ b/tablib/formats/__init__.py
@@ -7,5 +7,6 @@ import _csv as csv
import _json as json
import _xls as xls
import _yaml as yaml
+import _tsv as tsv
-available = (json, xls, yaml, csv)
+available = (json, xls, yaml, csv, tsv)
diff --git a/tablib/formats/_tsv.py b/tablib/formats/_tsv.py
new file mode 100644
index 0000000..8603c45
--- /dev/null
+++ b/tablib/formats/_tsv.py
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+
+""" Tablib - TSV (Tab Separated Values) Support.
+"""
+
+import cStringIO
+import csv
+import os
+
+import tablib
+
+
+title = 'tsv'
+extentions = ('tsv',)
+
+
+
+def export_set(dataset):
+ """Returns a TSV representation of Dataset."""
+ stream = cStringIO.StringIO()
+ _tsv = csv.writer(stream, delimiter="\t")
+
+ for row in dataset._package(dicts=False):
+ _tsv.writerow(row)
+
+ return stream.getvalue()
+
+
+def import_set(dset, in_stream, headers=True):
+ """Returns dataset from TSV stream."""
+
+ dset.wipe()
+
+ rows = csv.reader(in_stream.split("\r\n"), delimiter="\t")
+ for i, row in enumerate(rows):
+
+ # Skip empty rows
+ if not row:
+ continue
+
+ if (i == 0) and (headers):
+ dset.headers = row
+ else:
+ dset.append(row)
+
+
+def detect(stream):
+ """Returns True if given stream is valid TSV."""
+ try:
+ rows = dialect = csv.Sniffer().sniff(stream, delimiters="\t")
+ return True
+ except csv.Error:
+ return False