<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
	<id>https://indywiki.fr/index.php?action=history&amp;feed=atom&amp;title=Discussion%3ASplitCDR</id>
	<title>Discussion:SplitCDR - Historique des versions</title>
	<link rel="self" type="application/atom+xml" href="https://indywiki.fr/index.php?action=history&amp;feed=atom&amp;title=Discussion%3ASplitCDR"/>
	<link rel="alternate" type="text/html" href="https://indywiki.fr/index.php?title=Discussion:SplitCDR&amp;action=history"/>
	<updated>2026-04-18T10:16:21Z</updated>
	<subtitle>Historique des versions pour cette page sur le wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://indywiki.fr/index.php?title=Discussion:SplitCDR&amp;diff=3533&amp;oldid=prev</id>
		<title>Anthony le 28 mars 2024 à 14:13</title>
		<link rel="alternate" type="text/html" href="https://indywiki.fr/index.php?title=Discussion:SplitCDR&amp;diff=3533&amp;oldid=prev"/>
		<updated>2024-03-28T14:13:39Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nouvelle page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
import os&lt;br /&gt;
import csv&lt;br /&gt;
from datetime import timedelta&lt;br /&gt;
&lt;br /&gt;
# Fonction pour convertir la durée en format HH:MM:SS&lt;br /&gt;
def convert_to_hms(duration):&lt;br /&gt;
    duration_seconds = int(duration)&lt;br /&gt;
    hours, remainder = divmod(duration_seconds, 3600)&lt;br /&gt;
    minutes, seconds = divmod(remainder, 60)&lt;br /&gt;
    return f&amp;quot;{hours}h{minutes}m{seconds}s&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Fonction pour convertir les kilooctets en gigaoctets et arrondir au centième&lt;br /&gt;
def convert_to_gb(data_volume):&lt;br /&gt;
    return round(float(data_volume) / 1000000, 2)  # 1 gigaoctet = 1 000 000 kilooctets&lt;br /&gt;
&lt;br /&gt;
# Fonction pour agréger les données par client et caller&lt;br /&gt;
def aggregate_data(data):&lt;br /&gt;
    client_data = {}&lt;br /&gt;
    for row in data:&lt;br /&gt;
        client_name = row[&amp;#039;client_name&amp;#039;]&lt;br /&gt;
        caller = row[&amp;#039;caller&amp;#039;]&lt;br /&gt;
        if client_name not in client_data:&lt;br /&gt;
            client_data[client_name] = {}&lt;br /&gt;
&lt;br /&gt;
        if caller not in client_data[client_name]:&lt;br /&gt;
            client_data[client_name][caller] = {&lt;br /&gt;
                &amp;#039;caller&amp;#039;: caller,&lt;br /&gt;
                &amp;#039;duration&amp;#039;: 0.0,&lt;br /&gt;
                &amp;#039;price&amp;#039;: 0.0,&lt;br /&gt;
                &amp;#039;sva_service_cost&amp;#039;: 0.0,&lt;br /&gt;
                &amp;#039;data_volume&amp;#039;: 0.0,&lt;br /&gt;
                &amp;#039;categories&amp;#039;: {}  # Dictionnaire pour stocker les résultats par catégorie&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
        client_data[client_name][caller][&amp;#039;duration&amp;#039;] += float(row[&amp;#039;duration&amp;#039;]) if row[&amp;#039;duration&amp;#039;] else 0.0&lt;br /&gt;
        client_data[client_name][caller][&amp;#039;price&amp;#039;] += float(row[&amp;#039;price&amp;#039;]) if row[&amp;#039;price&amp;#039;] else 0.0&lt;br /&gt;
        client_data[client_name][caller][&amp;#039;sva_service_cost&amp;#039;] += float(row[&amp;#039;sva_service_cost&amp;#039;]) if row[&amp;#039;sva_service_cost&amp;#039;] else 0.0&lt;br /&gt;
        client_data[client_name][caller][&amp;#039;data_volume&amp;#039;] += float(row[&amp;#039;data_volume&amp;#039;]) if row[&amp;#039;data_volume&amp;#039;] else 0.0&lt;br /&gt;
        &lt;br /&gt;
        # Mettre à jour les résultats par catégorie&lt;br /&gt;
        category = row[&amp;#039;category&amp;#039;]&lt;br /&gt;
        client_data[client_name][caller][&amp;#039;categories&amp;#039;][category] = client_data[client_name][caller][&amp;#039;categories&amp;#039;].get(category, 0) + 1&lt;br /&gt;
&lt;br /&gt;
    return client_data&lt;br /&gt;
&lt;br /&gt;
# Fonction pour écrire les données agrégées dans un fichier CSV&lt;br /&gt;
def write_aggregated_data(client_data, file_name):&lt;br /&gt;
    for client_name, client_info in client_data.items():&lt;br /&gt;
        output_dir = os.path.splitext(file_name)[0] + &amp;quot;_split&amp;quot;&lt;br /&gt;
        os.makedirs(output_dir, exist_ok=True)&lt;br /&gt;
        &lt;br /&gt;
        # Écrire le fichier résultant pour chaque client&lt;br /&gt;
        output_file = os.path.join(output_dir, f&amp;quot;{client_name}.csv&amp;quot;)&lt;br /&gt;
        with open(output_file, &amp;#039;w&amp;#039;, newline=&amp;#039;&amp;#039;, encoding=&amp;#039;utf-8&amp;#039;) as file:&lt;br /&gt;
            writer = csv.writer(file, delimiter=&amp;#039;;&amp;#039;)&lt;br /&gt;
            # Récupérer toutes les colonnes possibles&lt;br /&gt;
            all_columns = set()&lt;br /&gt;
            for caller_info in client_info.values():&lt;br /&gt;
                all_columns.update(caller_info[&amp;#039;categories&amp;#039;].keys())&lt;br /&gt;
            &lt;br /&gt;
            # Supprimer les colonnes avec des données non significatives (toutes les valeurs à zéro)&lt;br /&gt;
            significant_columns = [col for col in all_columns if any(caller_info[&amp;#039;categories&amp;#039;].get(col, 0) != 0 for caller_info in client_info.values())]&lt;br /&gt;
&lt;br /&gt;
            # Écrire l&amp;#039;en-tête avec les colonnes significatives&lt;br /&gt;
            header = [&amp;#039;caller&amp;#039;, &amp;#039;duration&amp;#039;, &amp;#039;price&amp;#039;, &amp;#039;sva_service_cost&amp;#039;, &amp;#039;data_volume&amp;#039;] + list(significant_columns)&lt;br /&gt;
            writer.writerow(header)&lt;br /&gt;
            # Écrire les données agrégées pour chaque caller du client&lt;br /&gt;
            for caller, caller_info in client_info.items():&lt;br /&gt;
                caller_info[&amp;#039;duration&amp;#039;] = convert_to_hms(caller_info[&amp;#039;duration&amp;#039;])&lt;br /&gt;
                caller_info[&amp;#039;data_volume&amp;#039;] = convert_to_gb(caller_info[&amp;#039;data_volume&amp;#039;])&lt;br /&gt;
                row = [&lt;br /&gt;
                    caller_info[&amp;#039;caller&amp;#039;],&lt;br /&gt;
                    caller_info[&amp;#039;duration&amp;#039;],&lt;br /&gt;
                    caller_info[&amp;#039;price&amp;#039;],&lt;br /&gt;
                    caller_info[&amp;#039;sva_service_cost&amp;#039;],&lt;br /&gt;
                    caller_info[&amp;#039;data_volume&amp;#039;]&lt;br /&gt;
                ]&lt;br /&gt;
                # Ajouter les résultats par catégorie à la ligne&lt;br /&gt;
                for category in significant_columns:&lt;br /&gt;
                    row.append(caller_info[&amp;#039;categories&amp;#039;].get(category, 0))&lt;br /&gt;
                writer.writerow(row)&lt;br /&gt;
&lt;br /&gt;
    print(&amp;quot;Les fichiers traités ont été enregistrés avec succès dans le répertoire:&amp;quot;, output_dir)&lt;br /&gt;
&lt;br /&gt;
# Recherche de fichiers CSV dans le même répertoire que le script&lt;br /&gt;
csv_files = [file for file in os.listdir() if file.endswith(&amp;#039;.csv&amp;#039;)]&lt;br /&gt;
if not csv_files:&lt;br /&gt;
    print(&amp;quot;Aucun fichier CSV trouvé dans le répertoire.&amp;quot;)&lt;br /&gt;
    exit()&lt;br /&gt;
&lt;br /&gt;
for file_name in csv_files:&lt;br /&gt;
    # Lire le fichier CSV en spécifiant le séparateur&lt;br /&gt;
    data = []&lt;br /&gt;
    with open(file_name, &amp;#039;r&amp;#039;, newline=&amp;#039;&amp;#039;, encoding=&amp;#039;utf-8&amp;#039;) as file:&lt;br /&gt;
        reader = csv.DictReader(file, delimiter=&amp;#039;;&amp;#039;)&lt;br /&gt;
        for row in reader:&lt;br /&gt;
            # Exclure les lignes dont le &amp;quot;caller&amp;quot; ne commence pas par &amp;#039;06&amp;#039; ou &amp;#039;07&amp;#039; ou contient un &amp;quot;master_number&amp;quot;&lt;br /&gt;
            if row[&amp;#039;caller&amp;#039;].startswith((&amp;#039;06&amp;#039;, &amp;#039;07&amp;#039;)) and not row[&amp;#039;master_number&amp;#039;]:&lt;br /&gt;
                data.append(row)&lt;br /&gt;
&lt;br /&gt;
    # Agréger les données par client et caller&lt;br /&gt;
    client_data = aggregate_data(data)&lt;br /&gt;
&lt;br /&gt;
    # Écrire les données agrégées dans un fichier CSV&lt;br /&gt;
    write_aggregated_data(client_data, file_name)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Anthony</name></author>
	</entry>
</feed>