Add wgexport.py
This commit is contained in:
45
wgmaster/wgexport.py
Normal file
45
wgmaster/wgexport.py
Normal file
@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import argparse
|
||||
import pypyodbc
|
||||
|
||||
#pypyodbc.lowercase = False
|
||||
|
||||
|
||||
def convert(a) -> str:
|
||||
if type(a) == str:
|
||||
return f'"{a}"'
|
||||
elif type(a) == bool:
|
||||
return "T" if a else "F"
|
||||
elif a is None:
|
||||
return ""
|
||||
else:
|
||||
return str(a)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('wgdaten')
|
||||
args = parser.parse_args()
|
||||
|
||||
conn = pypyodbc.connect(
|
||||
r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
|
||||
f"Dbq={args.wgdaten};")
|
||||
|
||||
cur = conn.cursor()
|
||||
|
||||
try:
|
||||
tbls = cur.tables(tableType='TABLE')
|
||||
tbls = tbls.fetchall()
|
||||
for file, _, t_name, t_type, _ in tbls:
|
||||
print(t_name)
|
||||
cur.execute(f"SELECT * FROM {t_name};")
|
||||
cols = [t[0] for t in cur.description]
|
||||
|
||||
with open(f'tables/{t_name}.csv', 'wb+') as f:
|
||||
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
||||
for row in cur:
|
||||
f.write((';'.join([convert(a) for a in row]) + '\n').encode('utf-8'))
|
||||
finally:
|
||||
cur.close()
|
||||
conn.close()
|
Reference in New Issue
Block a user