User Tools

Site Tools


procedures:vonage_billing_sms.zip
import glob
import os
import pandas as pd
import zipfile
folder_path = '.'

# funzione per trovare un nome file libero
def get_unique_filename(folder, filename):
    base, ext = os.path.splitext(filename)
    counter = 1
    new_filename = filename
    while os.path.exists(os.path.join(folder, new_filename)):
        new_filename = f"{base}({counter}){ext}"
        counter += 1
    return new_filename

# scorro tutti i file nella cartella
for filename in os.listdir(folder_path):
    if filename.endswith('.zip'):
        zip_path = os.path.join(folder_path, filename)
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            for member in zip_ref.namelist():
                # solo se è un file (saltiamo le cartelle)
                if not member.endswith('/'):
                    # calcolo un nome unico se serve
                    unique_name = get_unique_filename(folder_path, os.path.basename(member))
                    # estraggo il file col nuovo nome
                    with zip_ref.open(member) as source, open(os.path.join(folder_path, unique_name), 'wb') as target:
                        target.write(source.read())
# Lista per contenere i DataFrame
df_list = []

# Scorro tutti i file nella cartella
for filename in os.listdir(folder_path):
    # leggo solo i file .csv e creo i df
    if filename.endswith('.csv'):
        file_path = os.path.join(folder_path, filename)
        df = pd.read_csv(file_path, low_memory=False)
        df_list.append(df)

# concateno tutti i df
df = pd.concat(df_list, ignore_index=True)
#creo il dataframe
df = df[['account_id', 'total_price']]
df['account_id'] = df['account_id'].astype(str)
#associo ogni apikey al cliente
api_keys = {
    "e619da50": {
        "nome": "Arquati SMS"
    },
    "9ae6cba5": {
        "nome": "Arquati SMPP"
    },
    "0e5aba8c": {
        "nome": "BeEasy SMPP"
    },
    "5407f75b": {
        "nome": "BeEasy SMPP"
    },
    "a104a88f": {
        "nome": "BeEasy SMPP"
    },
    "c3b0922e": {
        "nome": "BeEasy SMPP"
    },
    "6b544373": {
        "nome": "BeEasy SMPP"
    },
    "362a9421": {
        "nome": "Careapt SMS"
    },
    "f0413881": {
        "nome": "Careapt SMPP"
    },
    "84581826": {
        "nome": "Estendo SMS"
    },
    "a6601403": {
        "nome": "Estendo SMPP"
    },
    "158f48a7": {
        "nome": "Estendo SMPP"
    },
    "2f022a02": {
        "nome": "Iveco SMPP"
    },
    "3d6700a7": {
        "nome": "Servicecredit SMS"
    },
    "a9d659b2": {
        "nome": "Servicecredit SMPP"
    }
}
#creo i dataframes per ogni apikey
account_ids = df["account_id"].unique()
for account_id in account_ids:
    df['account_id'] = df['account_id'].replace({account_id: api_keys[account_id]["nome"]})
dfs = [group for _, group in df.groupby('account_id')]
#faccio i conti e stampo
for d in dfs:
    print("\033[1m",d["account_id"].iloc[0],"\033[0m")
    print("\tSMS: ",len(d))
    print("\tCosto a noi: ", round(d["total_price"].sum(),2), "€")
procedures/vonage_billing_sms.zip.txt · Last modified: by luca.bottoli

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki