Files
videotron-invoice-compilator/videotron1.py
2023-05-29 12:57:49 -04:00

78 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
""" python script to read videotron invoice and sum the data usage per user.
"""
import pandas as pd
import PySimpleGUI as sg
sg.theme("Brownblue")
layout = [
[sg.T("")],
[sg.Text("Choisir le fichier CSV: "),
sg.Input(key="-IN2-", change_submits=True),
sg.FileBrowse(key="-IN-", file_types=(("CSV Files", "*.csv"),))],
[sg.Button("Submit")]
]
window = sg.Window('VIDEOTRON MOBILE DATA CALCULATOR', layout, size=(600, 150))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Exit":
break
elif event == "Submit":
pd.set_option('display.max_rows', None)
df = pd.read_csv(
values["-IN2-"], sep=";",
encoding='unicode_escape',
decimal=",",
usecols=[9, 20, 39]
)
df.replace('UTILISATION POUR LE', '', regex=True, inplace=True)
df = df.set_axis(['USER', 'B', 'DATA'], axis=1)
df = df.replace('é', 'e', regex=True)
df = df.replace('Ã', 'E', regex=True)
df = df.replace('E´', 'o', regex=True)
df = df.replace('', 'c', regex=True)
df = df.replace('‰', '', regex=True)
df = df[df['B'].str.contains('DONN')]
df['DATA'] = df['DATA'].apply(
lambda x: float(x.split()[0].replace(',', '.'))
)
datasum = df.groupby('USER')['DATA'].sum(
min_count=1).reset_index().sort_values(by='DATA')
total_users = len(datasum) # Nombre total d'utilisateurs
for index, row in datasum.iterrows():
total_data = round(row['DATA'])
user = row['USER']
if total_data > 1024:
total_data_gb = total_data / 1024
total_data_text = "{:.2f} GB".format(total_data_gb)
else:
total_data_text = "{} KB".format(total_data)
print("{} \t {}".format(user, total_data_text))
total_data = round(df['DATA'].sum())
if total_data > 1024:
total_data_gb = total_data / 1024
total_data_text = "{:.2f} GB".format(total_data_gb)
else:
total_data_text = "{} KB".format(total_data)
print("Total Users : {} \t Total Data Usage: {}".format(
total_users, total_data_text))