From b57c1d9e9de9694cfcfd2f17272fb5697eb45ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1=C4=8Dek?= Date: Tue, 5 Apr 2022 15:13:57 +0200 Subject: [PATCH] done --- tmgdsp.py | 86 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/tmgdsp.py b/tmgdsp.py index 9608eac..77c3488 100644 --- a/tmgdsp.py +++ b/tmgdsp.py @@ -1,8 +1,17 @@ import csv from datetime import datetime import sys +import os + +dirpath = "." +FILE = [filename for filename in os.listdir(dirpath) if filename.startswith("TMGDSP_")] +FILE = FILE[0] +ext_date_from_filaname = FILE.split('_')[-1] +print(ext_date_from_filaname) + + + -FILE = 'TMGDSP202112.CSV' #usrmonth = input("Month to be processed: ") @@ -31,7 +40,10 @@ def add_dictsms(id, pricesms): print("increasing sms dict key for dict: ",dict," for :",pricesms) dict["smscount"] += pricesms - +def add_dictdataamount(id, dataamount): + dict = dictionary.get(id) + print("increasing dataamount dict key for dict: ",dict," for :",dataamount) + dict["dataamount"] += int(dataamount)/1024 @@ -69,6 +81,8 @@ with open(FILE, newline='', encoding='utf-8') as csvfile: MSISDN = int(row['MSISDN']) ICCID = int(row['ICCID']) + IMSI = int(row['IMSI']) + print("charge_net: ",row['CHARGE_NET']) price = float(row['CHARGE_NET'].replace(',','.')) @@ -100,7 +114,7 @@ with open(FILE, newline='', encoding='utf-8') as csvfile: #only if not exist create new record in dictionary for ID if not MSISDN in dictionary: - dictionary[MSISDN] = {'ICCID':ICCID,'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms} + dictionary[MSISDN] = {'IMSI':IMSI, 'ICCID':ICCID,'data':0.00000, 'sms':0.00000, 'other':0.00000, 'dataamount':0, 'smscount':pricesms} print("Creating new dictionary: ", dictionary[MSISDN]) if dictionary[MSISDN] == "436761890300016": print("creating dictionary for our unit") @@ -121,6 +135,16 @@ with open(FILE, newline='', encoding='utf-8') as csvfile: print("increasing pricesms") + if row['INCREMENT'] == "1/1 kB": + + if row['RATED_AMOUNT'] != "0": + + dataamount = row['RATED_AMOUNT'] + + print(dataamount) + add_dictdataamount(MSISDN, dataamount) + else: + dataamount = 0 @@ -149,18 +173,50 @@ print("Count of units: ", len(dictionary.keys())) # f.write("%s,%s\n"%(key,dictionary[key])) +with open('TMGDSP_output_'+ext_date_from_filaname, 'w') as f: -csv_columns = ['takhleuztoneudelam', 'ICCID', 'data', 'sms', 'other', 'smscount'] -try: - with open('output_tmgdsp_202112.csv', 'w') as csvfile: - w = csv.DictWriter(sys.stdout, csv_columns) - for key, val in sorted(dictionary.items()): - row = {'takhleuztoneudelam': key} - row.update(val) + writer = csv.writer(f) + writer.writerow(["MSISDN","ICCID","IMSI","DATA-PRICE","DATA-AMOUNT","SMS-PRICE","SMSCOUNT","OTHER-PRICE"]) + for key in dictionary.keys(): + # Getting all required variables to write them into export - writer = csv.DictWriter(csvfile, fieldnames=csv_columns) - writer.writerow(row) + MSISDN = key + ICCID = dictionary[key]['ICCID'] + IMSI = dictionary[key]['IMSI'] + data_price = dictionary[key]['data'] + sms_price = dictionary[key]['sms'] + other_price = dictionary[key]['other'] + sms_count = dictionary[key]['smscount'] + data_amount = dictionary[key]['dataamount'] -except IOError: - print(f"I/O error: {IOError}") -csvfile.close() + + + print("MSISDN: ", MSISDN) + print("ICCID: ", ICCID) + print("IMSI: ", IMSI) + print("data_price: ", data_price) + print("sms_price: ", sms_price) + print("sms_count: ", sms_count) + writer.writerow([MSISDN, ICCID, IMSI, data_price, data_amount, sms_price, sms_count, other_price]) +{'ICCID':ICCID,'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms} +#csvfile.close() + + + + + + +#csv_columns = ['takhleuztoneudelam', 'ICCID', 'data', 'sms', 'other', 'smscount'] +#try: +# with open('output_tmgdsp_202112.csv', 'w') as csvfile: +# w = csv.DictWriter(sys.stdout, csv_columns) + # for key, val in sorted(dictionary.items()): + #row = {'takhleuztoneudelam': key} + #row.update(val) + + #writer = csv.DictWriter(csvfile, fieldnames=csv_columns) + #writer.writerow(row) + +#except IOError: + #print(f"I/O error: {IOError}") +#csvfile.close()