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): def to_string(self):
return ''.join(self.to_lines()) 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" "Parse data from file, string or line iterator, overwriting self"
if isinstance(source, str) or isinstance(source, unicode): if isinstance(source, str) or isinstance(source, unicode):
return self.parse_from(source.split('\n')) return self.parse_from(source.split('\n'))
@ -29,6 +29,7 @@ class OvvpFile(object):
# header # header
self.headers = {} self.headers = {}
if with_headers:
for r in it: for r in it:
if isinstance(r, str): if isinstance(r, str):
r = r.decode('utf8') r = r.decode('utf8')
@ -61,7 +62,7 @@ class OvvpFile(object):
def parse(source): def parse(source, with_headers=True):
o = OvvpFile() o = OvvpFile()
o.parse_from(source) o.parse_from(source, with_headers=with_headers)
return o return o

Loading…
Cancel
Save