Files
SUST/sustApp/import_transactions.py
T
2025-01-19 17:10:56 +01:00

56 lines
1.9 KiB
Python

# Importing libraries
from numpy import genfromtxt
from time import time
from datetime import datetime
import models
import os
from database import engine, SessionLocal
from pytz import timezone
def Load_Data(file_name):
#data = genfromtxt(file_name, delimiter=';', skip_header=1, converters={0: lambda s: str(s)})
#str2date = lambda x: datetime.strptime(x, '%d-%m-%y')
data = genfromtxt(file_name, delimiter=';', dtype=None, skip_header=1, encoding='utf-8') #
return data.tolist()
if __name__ == "__main__":
t = time()
db = SessionLocal()
models.Base.metadata.create_all(bind=engine)
try:
home_directory = os.path.expanduser('~')
source_csv = os.path.join(home_directory, "OneDrive", "Dokumente", "SUST", "UmsatzListe_AT493443700000113407_20241212_095142_mod_out.csv")
data = Load_Data(source_csv)
tz = timezone('UTC') # Replace with your desired timezone
for i in data:
dt = datetime.strptime(i[8], '%d.%m.%Y %H:%M:%S:%f')
ts_timestamp = tz.localize(dt)
record = models.Payments_CSV(**{
'pc_valuta_date' : datetime.strptime(i[0], '%d.%m.%Y').date(),
'pc_client' : i[1],
'pc_reference' : i[2],
'pc_intended' : i[3],
'pc_iban' : i[4],
'pc_entry_date' : datetime.strptime(i[5], '%d.%m.%Y').date(),
'pc_amount' : float(i[6]),
'pc_currency' : i[7],
'pc_timestamp' : ts_timestamp
})
db.add(record) #Add all the records
try:
db.commit() #Attempt to commit all the records
except Exception as e:
print(e)
except Exception as e:
print (e)
db.rollback() #Rollback the changes on error
finally:
db.close() #Close the connection
print("Time elapsed: " + str(time() - t) + " s.") #0.091s