Support for TSV-files. Unit-tested.

This commit is contained in:
Luca Beltrame
2010-10-19 10:45:54 +02:00
parent 3b0e0c7991
commit e75a00541d
4 changed files with 113 additions and 1 deletions
+14
View File
@@ -306,6 +306,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
+2 -1
View File
@@ -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)
+53
View File
@@ -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