From 9cbaf696ecd7acdd9c70ebd4b84e8e1354d62fd2 Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Fri, 15 May 2015 00:19:40 +0200 Subject: [PATCH] Utility pro DAKOS import --- seminar/dksdump/helpers.py | 22 ++++++++++++++++++++++ seminar/ovvpfile.py | 25 +++++++++++++------------ 2 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 seminar/dksdump/helpers.py diff --git a/seminar/dksdump/helpers.py b/seminar/dksdump/helpers.py new file mode 100644 index 00000000..45e83612 --- /dev/null +++ b/seminar/dksdump/helpers.py @@ -0,0 +1,22 @@ +from seminar import ovvpfile +import os + + +def read_all_tables(basedir): + tables = {} + for fn in os.listdir(basedir): + if fn.endswith('.csv'): + print "Reading %s ..." % (fn, ) + with open(os.path.join(basedir, fn), 'r') as f: + o = ovvpfile.parse(f, with_headers=False) + tables[fn[:-4]] = o.rows + print " %d lines, columns: %s" % (len(o.rows), ' '.join(o.columns), ) + return tables + +def matchrows(tab, key, val): + return [r for r in tab if r[key]==val] + +def onerow(tab, key, val): + t = matchrows(tab, key, val) + assert(len(t) == 1) + return t[0] diff --git a/seminar/ovvpfile.py b/seminar/ovvpfile.py index 24310053..46d34df3 100644 --- a/seminar/ovvpfile.py +++ b/seminar/ovvpfile.py @@ -20,7 +20,7 @@ class OvvpFile(object): def to_string(self): return ''.join(self.to_lines()) - def parse_from(self, source): + def parse_from(self, source, with_headers=True): "Parse data from file, string or line iterator, overwriting self" if isinstance(source, str) or isinstance(source, unicode): return self.parse_from(source.split('\n')) @@ -29,15 +29,16 @@ class OvvpFile(object): # header self.headers = {} - for r in it: - if isinstance(r, str): - r = r.decode('utf8') - assert isinstance(r, unicode) - r = r.rstrip('\n') - if r == u"": - break - k, v = r.split(u'\t', 1) - self.headers[k] = v + if with_headers: + for r in it: + if isinstance(r, str): + r = r.decode('utf8') + assert isinstance(r, unicode) + r = r.rstrip('\n') + if r == u"": + break + k, v = r.split(u'\t', 1) + self.headers[k] = v # columns r = it.next() @@ -61,7 +62,7 @@ class OvvpFile(object): -def parse(source): +def parse(source, with_headers=True): o = OvvpFile() - o.parse_from(source) + o.parse_from(source, with_headers=with_headers) return o