def mergeDbfFiles(files): filepath = os.path.split(files[0])[0] i = 0 #Counter for header catTable = [] for aDbf in files: print("currently on table = {0}".format(aDbf)) table = dbf.Table(aDbf) table.open() fields = dbf.get_fields(aDbf) fields.append("date_added") print("fields = {0}".format(fields)) dateAdded = datetime.date.fromtimestamp(os.path.getctime(aDbf)) print("date_added = {0}".format(dateAdded)) print("No records in table = {0}".format(len(table))) for rec in table: tmp = [] for item in rec: tmp.append(item) # print ("row added = {0}".format(tmp)) tmp.append(dateAdded) catTable.append(tmp) # if i == 0: # catTable.insert(0,fields) # i+=1 table.close() print("Length of catTable = {0}".format(len(catTable))) # print (catTable[:2]) return (catTable, fields)
def mergeDbfFiles(files): filepath = os.path.split(files[0])[0] i=0 #Counter for header catTable = [] for aDbf in files: print("currently on table = {0}".format(aDbf)) table = dbf.Table(aDbf) table.open() fields = dbf.get_fields(aDbf) fields.append("date_added") print("fields = {0}".format(fields)) dateAdded = datetime.date.fromtimestamp(os.path.getctime(aDbf)) print ("date_added = {0}".format(dateAdded)) print("No records in table = {0}".format(len(table))) for rec in table: tmp = [] for item in rec: tmp.append(item) # print ("row added = {0}".format(tmp)) tmp.append(dateAdded) catTable.append(tmp) # if i == 0: # catTable.insert(0,fields) # i+=1 table.close() print("Length of catTable = {0}".format(len(catTable))) # print (catTable[:2]) return (catTable, fields)
def read_xbase(filename, callback, *args, **kwargs): """ run the callback function for each row in the table (similar to xBase SCAN) """ flds = dbf.get_fields(filename) t = dbf.Table(filename) t.open('read-only') if kwargs.get('do_init'): del kwargs['do_init'] # to avoid error in the callback function init_import(args[0], cnt_total=len(t)) # args[0] ~ param for record in t: # callback(record, *args, **kwargs) # this run extremely slow !!! callback({fld: record[fld] for fld in flds}, *args, **kwargs) # with real python dict it run well t.close()
def methodDbfInsertCreating(self, sqltable, inpf, tmpf): """Функция получения INSERT запроса """ table = DbfTable() # Отражаем дбфку в экземпляр класса. table.methodDbfGetTable(tmpf) # Собираем схему как атрибуты экземпляра. table.methodDbfGetColumns(tmpf) # Кладем колонки в список с соответствующим атрибутом. tables = [] # Список схемы for i in table.__dict__.keys(): tables.append(i) array = [] # массив схемы, схема[колонка[значение,значение],колонка[значение,значение]...] for line in range(table.methodDbfGetLen()): if line == 0: continue row = [] for col in tables: row.append(table.__dict__[col][line]) array.append(row) fields = str(dbf.get_fields(inpf)).replace("[","(").replace("]",")").replace("'","") query = [] # Запросы for i in range(len(array)): query.append('INSERT INTO {0} {1} VALUES {2}'.format(sqltable,fields,str(array[i]).replace("[","(").replace("]",")"))+';') return query # Функция возвращает запрос
import os import dbf DBF_FILE_LIST = [] ##하위 경로 DBF 검색 filenames = os.listdir('./') for filename in filenames: full_filename = os.path.join('./', filename) if os.path.isdir(full_filename): search(full_filename) else: ext = os.path.splitext(full_filename)[-1] if ext == '.dbf': print(filename) DBF_FILE_LIST.append(filename) print(DBF_FILE_LIST) table = dbf.Table('77-2.dbf') table.open(mode=dbf.READ_WRITE) t = dbf.get_fields('77-2.dbf') print(t) for DBF in DBF_FILE_LIST: print('getting', DBF) table = dbf.Table(DBF) table.open(mode=dbf.READ_WRITE) t = dbf.get_fields(DBF) print(t)
ku = int(dbf_subor.lower()[-10:-4]) if not os.path.exists( dbf_subor_full_path) or dbf_subor[-3:].lower() != 'dbf': raise Exception except: print(f"-- E: Neplatny subor DBF ({dbf_subor}).\n") print(__doc__) sys.exit(2) # Nacitanie DBF suboru dbfile = dbf.Table(dbf_subor_full_path) dbfile.open() # Nacitanie nazvov stlpcov ([:-1] odstranuje stlpec CRC) zoznam_stlpcov = dbf.get_fields(dbf_subor_full_path)[:-1] numdata = { 'bp': [ 'icp', 'clv', 'pcs', 'bnp', 'cip', 'cib', 'vym', 'pec', 'mss', 'cit', 'men', 'pvz', 'kpv', 'cen', 'oce', 'crc' ], 'cs': [ 'ics', 'clv', 'cel', 'cpa', 'pec', 'mss', 'vym', 'ums', 'drs', 'pvz', 'kpv', 'don', 'cen', 'oce', 'zcs', 'crc' ], 'ep': [ 'cpa', 'cpu', 'vym', 'kvv', 'drp', 'don', 'pkk', 'mss', 'pec', 'cel', 'clv', 'kpv', 'ump', 'pvz', 'miv', 'clm', 'drv', 'ndp', 'prp', 'spn', 'mlm', 'cen', 'oce', 'pcp', 'crc' ],
def run(self): caller = self.caller statusbar_context_id = caller.statusbar.get_context_id('ReadDbf Thread') if caller.scrolled_window: print datetime.today(), "destroying old visualization" gobject.idle_add(caller.scrolled_window.destroy) print datetime.today(), "old visualization destroyed" print datetime.today(), "opening dbf file" gobject.idle_add(caller.statusbar.push, statusbar_context_id, 'opening %s' % caller.dbf_file) try: caller.dbf_table = dbf.Table(caller.dbf_file, read_only = True) except: self.finished = true; self.failed = true; return print datetime.today(), "dbf file opened" caller.dbf_length = len(caller.dbf_table) print datetime.today(), "retrieving fields" fields = dbf.get_fields(caller.dbf_table) print datetime.today(), "fields retrieved" print datetime.today(), "creating new visualization" store_param= [str] * len(fields) store_param.insert(0, int) store = gtk.ListStore(*store_param) caller.list_view = gtk.TreeView(store) caller.list_view.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL) caller.scrolled_window = gtk.ScrolledWindow() caller.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) caller.scrolled_window.add(caller.list_view) caller.content_box.pack_start(caller.scrolled_window, True, True, 0) i = 0 print datetime.today(), "populating list view" column = gtk.TreeViewColumn("No.") caller.list_view.append_column(column) cell = gtk.CellRendererText() cell.set_alignment(1, 0) column.pack_start(cell, True) column.add_attribute(cell, 'text', i) print datetime.today(), "creating columns for fields" for field_name in fields: i += 1 column = gtk.TreeViewColumn(field_name) caller.list_view.append_column(column) cell = gtk.CellRendererText() column.pack_start(cell, True) column.add_attribute(cell, 'text', i) column.set_resizable(True) print datetime.today(), "retrieving old model" model = caller.list_view.get_model() print datetime.today(), "unset model" caller.list_view.set_model() caller.row_count = 0 print datetime.today(), "iterating table" for row in caller.dbf_table: caller.row_count += 1 try: data = list(row) data.insert(0, caller.row_count) model.append(data) except: print datetime.today(), 'error detected (64b68)' print row #time.sleep(1) print datetime.today(), "setting list view model" caller.list_view.set_model(model) print datetime.today(), "queue to show list view" gobject.idle_add(caller.list_view.show) print datetime.today(), "queue to show scrolled window" gobject.idle_add(caller.scrolled_window.show) print datetime.today(), "queue to destroy progress window" gobject.idle_add(caller.progress_window.destroy) print datetime.today(), "removing progress pulse" gobject.source_remove(caller.progress_timeout_source_id) gobject.idle_add(caller.statusbar.push, statusbar_context_id, "%s (%d rows)" % (os.path.basename(caller.dbf_file), caller.dbf_length)) gobject.idle_add(caller.table_info_menu_item.set_sensitive, True) print datetime.today(), 'finished on %f seconds' % (time.time() - caller.open_dbf_start_time) self.finished = True
def run(self): caller = self.caller statusbar_context_id = caller.statusbar.get_context_id( 'ReadDbf Thread') if caller.scrolled_window: print datetime.today(), "destroying old visualization" gobject.idle_add(caller.scrolled_window.destroy) print datetime.today(), "old visualization destroyed" print datetime.today(), "opening dbf file" gobject.idle_add(caller.statusbar.push, statusbar_context_id, 'opening %s' % caller.dbf_file) try: caller.dbf_table = dbf.Table(caller.dbf_file, read_only=True) except: self.finished = true self.failed = true return print datetime.today(), "dbf file opened" caller.dbf_length = len(caller.dbf_table) print datetime.today(), "retrieving fields" fields = dbf.get_fields(caller.dbf_table) print datetime.today(), "fields retrieved" print datetime.today(), "creating new visualization" store_param = [str] * len(fields) store_param.insert(0, int) store = gtk.ListStore(*store_param) caller.list_view = gtk.TreeView(store) caller.list_view.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL) caller.scrolled_window = gtk.ScrolledWindow() caller.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) caller.scrolled_window.add(caller.list_view) caller.content_box.pack_start(caller.scrolled_window, True, True, 0) i = 0 print datetime.today(), "populating list view" column = gtk.TreeViewColumn("No.") caller.list_view.append_column(column) cell = gtk.CellRendererText() cell.set_alignment(1, 0) column.pack_start(cell, True) column.add_attribute(cell, 'text', i) print datetime.today(), "creating columns for fields" for field_name in fields: i += 1 column = gtk.TreeViewColumn(field_name) caller.list_view.append_column(column) cell = gtk.CellRendererText() column.pack_start(cell, True) column.add_attribute(cell, 'text', i) column.set_resizable(True) print datetime.today(), "retrieving old model" model = caller.list_view.get_model() print datetime.today(), "unset model" caller.list_view.set_model() caller.row_count = 0 print datetime.today(), "iterating table" for row in caller.dbf_table: caller.row_count += 1 try: data = list(row) data.insert(0, caller.row_count) model.append(data) except: print datetime.today(), 'error detected (64b68)' print row #time.sleep(1) print datetime.today(), "setting list view model" caller.list_view.set_model(model) print datetime.today(), "queue to show list view" gobject.idle_add(caller.list_view.show) print datetime.today(), "queue to show scrolled window" gobject.idle_add(caller.scrolled_window.show) print datetime.today(), "queue to destroy progress window" gobject.idle_add(caller.progress_window.destroy) print datetime.today(), "removing progress pulse" gobject.source_remove(caller.progress_timeout_source_id) gobject.idle_add( caller.statusbar.push, statusbar_context_id, "%s (%d rows)" % (os.path.basename(caller.dbf_file), caller.dbf_length)) gobject.idle_add(caller.table_info_menu_item.set_sensitive, True) print datetime.today(), 'finished on %f seconds' % ( time.time() - caller.open_dbf_start_time) self.finished = True