|
| 1 | +import pandas as pd |
| 2 | +import sqlite3 |
| 3 | +from tkinter import messagebox |
| 4 | +import re |
| 5 | +from Database.CommonSqliteActions import CommonSqliteActions |
| 6 | + |
| 7 | +def fidelityHoldingsImport(csv_file_location): |
| 8 | + sql = CommonSqliteActions() |
| 9 | + |
| 10 | + try: |
| 11 | + sql.cursor.execute("DELETE FROM fidelity_portfolio") |
| 12 | + |
| 13 | + panda_store = pd.read_csv(csv_file_location, encoding="utf-8-sig").fillna(0) |
| 14 | + |
| 15 | + for index, row in panda_store.iterrows(): |
| 16 | + if(row['Symbol'] != 0): |
| 17 | + if re.search("[*\d\s]", row['Symbol']) is None: |
| 18 | + total_return = row['Total Gain/Loss Dollar'].translate({ord(i): None for i in "$+,)"}).replace('(', '-') |
| 19 | + equity = row['Cost Basis'].translate({ord(i): None for i in "$,"}) |
| 20 | + total_return_percent = row['Total Gain/Loss Percent'].translate({ord(i): None for i in "%+"}) |
| 21 | + allocation = row['Percent Of Account'].replace('%', '') |
| 22 | + |
| 23 | + insert_statement = "INSERT INTO fidelity_portfolio (ticker, name, shares, return, equity, percent_gain, percent_allocation) VALUES (\"{}\", \"{}\", {}, {}, {}, {}, {})".format(row['Symbol'], row['Description'], row['Quantity'], total_return, equity, total_return_percent, allocation) |
| 24 | + sql.cursor.execute(insert_statement) |
| 25 | + |
| 26 | + sql.closeDb() |
| 27 | + |
| 28 | + messagebox.showinfo(title="Success", message="Table successfully updated.") |
| 29 | + |
| 30 | + except sqlite3.OperationalError as error: |
| 31 | + messagebox.showerror(title="Operational error", message=error.args) |
| 32 | + except sqlite3.Error as error: |
| 33 | + messagebox.showerror(title="Database error", message=error.args) |
| 34 | + except TypeError as error: |
| 35 | + messagebox.showerror(title="Type error", message=error.args) |
| 36 | + except KeyError as error: |
| 37 | + messagebox.showerror(title="Mismatching key", message = "The key can not be found in the file; check you are uploading the correct file or check format. Key:" + str(error.args)) |
| 38 | + except ValueError: |
| 39 | + messagebox.showerror(title="No File Found", message="Invalid file path or no file selected") |
0 commit comments