Refactor python files
This commit is contained in:
@ -1,32 +1,14 @@
|
||||
#!/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Any
|
||||
import argparse
|
||||
import datetime
|
||||
import os
|
||||
import pypyodbc
|
||||
|
||||
|
||||
def convert(tbl: str, name: str, a: Any) -> str:
|
||||
if type(a) == str:
|
||||
return f'"{a}"'
|
||||
elif type(a) == bool:
|
||||
return "T" if a else "F"
|
||||
elif a is None:
|
||||
return ""
|
||||
elif type(a) == datetime.datetime:
|
||||
if a.year == 1899 and a.month == 12 and a.day == 30:
|
||||
return a.strftime('%H:%M:%S')
|
||||
elif a.hour == 0 and a.minute == 0 and a.second == 0:
|
||||
return a.strftime('%Y-%m-%d')
|
||||
else:
|
||||
return str(a)
|
||||
else:
|
||||
return str(a)
|
||||
import csv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-o', '--output', default='tables')
|
||||
parser.add_argument('wgdaten', metavar='WGDATEN')
|
||||
@ -61,9 +43,14 @@ if __name__ == '__main__':
|
||||
with open(f'{args.output}/{t_name}.csv', 'wb+') as f:
|
||||
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
||||
for row in cur:
|
||||
f.write((';'.join([convert(t_name, n, a) for n, a in zip(cols, row)]) + '\n').encode('utf-8'))
|
||||
values = [csv.convert_value(val, table=t_name, column=col) for col, val in zip(cols, row)]
|
||||
f.write((';'.join(values) + '\n').encode('utf-8'))
|
||||
|
||||
print(f'Exported {t_name} successfully!', flush=True)
|
||||
finally:
|
||||
cur.close()
|
||||
cnx.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Reference in New Issue
Block a user