Added output argument to wgexport.py
This commit is contained in:
@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import datetime
|
import datetime
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
import decimal
|
import decimal
|
||||||
import pypyodbc
|
import pypyodbc
|
||||||
|
|
||||||
|
|
||||||
def convert(a) -> str:
|
def convert(n, a) -> str:
|
||||||
if type(a) == str:
|
if type(a) == str:
|
||||||
return f'"{a}"'
|
return f'"{a}"'
|
||||||
elif type(a) == bool:
|
elif type(a) == bool:
|
||||||
@ -26,9 +28,13 @@ def convert(a) -> str:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('-o', '--output', default='tables')
|
||||||
parser.add_argument('wgdaten')
|
parser.add_argument('wgdaten')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
shutil.rmtree(args.output)
|
||||||
|
os.makedirs(args.output, exist_ok=True)
|
||||||
|
|
||||||
pypyodbc.lowercase = False
|
pypyodbc.lowercase = False
|
||||||
conn = pypyodbc.connect(f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={args.wgdaten};")
|
conn = pypyodbc.connect(f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={args.wgdaten};")
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
@ -38,17 +44,19 @@ if __name__ == '__main__':
|
|||||||
tbls = tbls.fetchall()
|
tbls = tbls.fetchall()
|
||||||
for file, _, t_name, t_type, _ in tbls:
|
for file, _, t_name, t_type, _ in tbls:
|
||||||
print(t_name)
|
print(t_name)
|
||||||
|
|
||||||
cur.execute(f"SELECT TOP 1 * FROM {t_name};")
|
cur.execute(f"SELECT TOP 1 * FROM {t_name};")
|
||||||
desc = [(t[0], t[1]) for t in cur.description]
|
desc = [(t[0], t[1]) for t in cur.description]
|
||||||
cur.fetchall()
|
cur.fetchall()
|
||||||
print(desc)
|
print(desc)
|
||||||
|
|
||||||
cur.execute(f"SELECT * FROM {t_name} ORDER BY `{desc[0][0]}`;")
|
cur.execute(f"SELECT * FROM {t_name} ORDER BY `{desc[0][0]}`;")
|
||||||
cols = [t[0] for t in cur.description]
|
cols = [t[0] for t in cur.description]
|
||||||
|
|
||||||
with open(f'tables/{t_name}.csv', 'wb+') as f:
|
with open(f'{args.output}/{t_name}.csv', 'wb+') as f:
|
||||||
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
||||||
for row in cur:
|
for row in cur:
|
||||||
f.write((';'.join([convert(a) for a in row]) + '\n').encode('utf-8'))
|
f.write((';'.join([convert(n, a) for n, a in zip(cols, row)]) + '\n').encode('utf-8'))
|
||||||
finally:
|
finally:
|
||||||
cur.close()
|
cur.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
Reference in New Issue
Block a user