Browse Source

Utility pro DAKOS import

remotes/origin/feincms
Tomas Gavenciak 10 years ago
parent
commit
9cbaf696ec
  1. 22
      seminar/dksdump/helpers.py
  2. 25
      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]

25
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,15 +29,16 @@ class OvvpFile(object):
# header # header
self.headers = {} self.headers = {}
for r in it: if with_headers:
if isinstance(r, str): for r in it:
r = r.decode('utf8') if isinstance(r, str):
assert isinstance(r, unicode) r = r.decode('utf8')
r = r.rstrip('\n') assert isinstance(r, unicode)
if r == u"": r = r.rstrip('\n')
break if r == u"":
k, v = r.split(u'\t', 1) break
self.headers[k] = v k, v = r.split(u'\t', 1)
self.headers[k] = v
# columns # columns
r = it.next() r = it.next()
@ -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