Dyn Col:index. New Errors. Dynamic Col:Names. Remove Old Generated. Dynamic new column position

This commit is contained in:
2022-09-12 13:45:22 +02:00
parent 2b15225b73
commit a57de4c348
2 changed files with 23 additions and 8 deletions

Binary file not shown.

View File

@@ -1,3 +1,5 @@
import os
from posixpath import dirname
import pandas as pd import pandas as pd
from datetime import datetime from datetime import datetime
import calendar import calendar
@@ -12,15 +14,15 @@ class Klinika(Enum):
URGJENCA = 'urgjenca' URGJENCA = 'urgjenca'
klinika = Klinika.KIRURGJI.value klinika = Klinika.OBS.value
xlsx_path = './Excel/' xlsx_path = './Excel/'
excel_path = join(xlsx_path, "raw_{klinika}_2016-2019.xlsx") excel_path = join(xlsx_path, f"raw_{klinika}_2016-2019.xlsx")
df = pd.read_excel(excel_path, sheet_name='Sheet1') df = pd.read_excel(excel_path, sheet_name='Sheet1')
df = df.reset_index() df = df.reset_index()
df['MOSHA'] = [None] * 2384 df['MOSHA'] = [None] * df.index.stop
df['ERROR'] = [None] * 2384 df['ERROR'] = [None] * df.index.stop
i = 0 i = 0
datat_pattern = "%d/%m/%y" datat_pattern = "%d/%m/%y"
data_shtri_korigj = [] data_shtri_korigj = []
@@ -49,6 +51,9 @@ def konverto_data(datat: str, i: int):
datat = int(datat) / 12 datat = int(datat) / 12
df.iat[i, df.columns.get_loc('MOSHA')] = float(datat) df.iat[i, df.columns.get_loc('MOSHA')] = float(datat)
return "DEKLARUAR" return "DEKLARUAR"
if datat.endswith('/'):
datat = datat[:-1]
print(datat)
datat = datat.split('/') datat = datat.split('/')
if len(datat) == 1: if len(datat) == 1:
datat.insert(0, '1') datat.insert(0, '1')
@@ -92,7 +97,10 @@ def pastro_string(datat: str, i: int):
for index, row in df.iterrows(): for index, row in df.iterrows():
data_shtri = row['DATA E SHTRIMIT'] try:
data_shtri = row['DATA E SHTRIMIT']
except KeyError as e:
data_shtri = row['DATA']
datelindja = row['DATELINDJA'] datelindja = row['DATELINDJA']
emer = row['EMER'] emer = row['EMER']
data_shtri = pastro_string(data_shtri, i) data_shtri = pastro_string(data_shtri, i)
@@ -111,12 +119,19 @@ for index, row in df.iterrows():
datelindja_korigj.append(datelindja) datelindja_korigj.append(datelindja)
i += 1 i += 1
df.insert(16, "DT_SHTRIMI_KORIGJ", data_shtri_korigj) df.insert(len(df.columns), "DT_SHTRIMI_KORIGJ", data_shtri_korigj)
df.insert(17, "DATELINDJA_KORIGJ", datelindja_korigj) df.insert(len(df.columns), "DATELINDJA_KORIGJ", datelindja_korigj)
df.drop(columns=df.columns[0], axis=1, inplace=True) df.drop(columns=df.columns[0], axis=1, inplace=True)
current_GMT = time.gmtime() current_GMT = time.gmtime()
ts = calendar.timegm(current_GMT) ts = calendar.timegm(current_GMT)
excel_path = join(xlsx_path, "{klinika}_{ts}.xlsx") for dirpath, dirname, filename in os.walk(xlsx_path):
for name in filename:
if name.startswith(klinika):
file_per_tu_fshire = join(xlsx_path, name)
os.remove(file_per_tu_fshire)
excel_path = join(xlsx_path, f"{klinika}_{ts}.xlsx")
df.to_excel(excel_path, index=False) df.to_excel(excel_path, index=False)