Browse Source

Utility pro DAKOS import

remotes/origin/feincms
Tomas Gavenciak 10 years ago
parent
commit
9cbaf696ec
  1. 22
      seminar/dksdump/helpers.py
  2. 7
      seminar/ovvpfile.py

22
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]

7
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,6 +29,7 @@ class OvvpFile(object):
# header
self.headers = {}
if with_headers:
for r in it:
if isinstance(r, str):
r = r.decode('utf8')
@ -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

Loading…
Cancel
Save