def test_rows_as_ordered_dict(self): rows = [OrderedDict([('s', 'life'), ('n', 1.0)]), OrderedDict([('s', 'universe'), ('n', 3.14)]), OrderedDict([('s', 'everything'), ('n', 42.0)])] fp = BytesIO() xport.from_rows(rows, fp) fp.seek(0) dup = list(xport.DictReader(fp)) self.assertEqual(rows, dup)
def test_from_rows(self, library): ds = next(iter(library.values())) rows = list(ds.itertuples(index=None, name=None)) fp = BytesIO() with pytest.warns(DeprecationWarning): xport.from_rows(rows, fp) fp.seek(0) result = next(iter(xport.v56.load(fp).values())) assert (result.values == ds.values).all(axis=None)
def test_rows_as_ordered_dict(self): rows = [ OrderedDict([('s', 'life'), ('n', 1.0)]), OrderedDict([('s', 'universe'), ('n', 3.14)]), OrderedDict([('s', 'everything'), ('n', 42.0)]) ] fp = BytesIO() xport.from_rows(rows, fp) fp.seek(0) dup = list(xport.DictReader(fp)) self.assertEqual(rows, dup)
def roundtrip(self, rows): fp = BytesIO() xport.from_rows(rows, fp) fp.seek(0) duplicate = xport.to_rows(fp) self.assertEqual(rows, duplicate)
import xport import csv import sys fields = [] rows = [] with open(sys.argv[1], 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: rows.append(row) records = [] for row in rows: i=0 dict = {} for col in row: dict[fields[i]] = col i = i + 1 records.append(dict) with open(sys.argv[2], 'wb') as f: xport.from_rows(records, f)