From 5314fb1d2b1c15828435a622bfb6bfe52e2a81b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1=C4=8Dek?= Date: Tue, 19 Apr 2022 22:11:07 +0200 Subject: [PATCH] export standardized --- ucetnictvi_vfcz.py | 68 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/ucetnictvi_vfcz.py b/ucetnictvi_vfcz.py index 0d0f1a7..381cdda 100644 --- a/ucetnictvi_vfcz.py +++ b/ucetnictvi_vfcz.py @@ -4,8 +4,14 @@ import csv from datetime import datetime import sys from pprint import pprint +import os -FILE = 'VFCZ202109.CSV' +dirpath = "." +FILE = [filename for filename in os.listdir(dirpath) if filename.startswith("VFCZ_")] +FILE = FILE[0] + +ext_date_from_filaname = FILE.split('_')[-1] +print(ext_date_from_filaname) #usrmonth = input("Month to be processed: ") #usrmonth = int(usrmonth) @@ -27,6 +33,15 @@ 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) + + dataamount = round(float(dataamount)/1024, 2) + + print("increasing dataamount dict key for dict: ",dict," for :",dataamount) + dict["dataamount"] += dataamount + #MAIN print("Start") @@ -86,7 +101,6 @@ with open(FILE, newline='', encoding="utf-8") as csvfile: - if row['direction'] != "I" and row['productser'] == "SMS ": pricesms = int(row['time_taken']) @@ -100,11 +114,12 @@ 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: print("Creating dictionary: ", MSISDN) - dictionary[MSISDN] = {'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms} + dictionary[MSISDN] = {'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms, 'dataamount':0.00} if price != '': print("Editing dictionary: ", MSISDN, "typ: ", typ, "cena: ", price) add_dict(MSISDN, typ, price) + add_dictdataamount(MSISDN, row['dataamount']) if row['direction'] != "I" and row['productser'] == "SMS ": print("pricesms: ", pricesms, " for unit: ", dictionary[MSISDN]) @@ -128,23 +143,44 @@ with open(FILE, newline='', encoding="utf-8") as csvfile: print("Count of units: ", len(dictionary.keys())) -with open('export_vd202109v1.csv', 'w') as f: + + +with open('output_VFCZ_'+ext_date_from_filaname, 'w') as f: + + writer = csv.writer(f) + writer.writerow(["MSISDN","ICCID","IMSI","DATA-PRICE","DATA-AMOUNT","SMS-PRICE","SMS-COUNT","OTHER-PRICE","SIM-STATUS"]) for key in dictionary.keys(): - f.write("%s,%s\n"%(key,dictionary[key])) + print(key) + print(dictionary[key]) + data = dictionary[key]['data'] + sms = dictionary[key]['sms'] + other = dictionary[key]['other'] + smscount = dictionary[key]['smscount'] + dataamount = dictionary[key]['dataamount'] -csv_columns = ['takhleuztoneudelam', 'data', 'sms', 'other', 'smscount'] -try: - with open('export_vd_202109v1.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) + writer.writerow([key, "N/A", "N/A", data, dataamount, sms, smscount, other, "N/A"]) -except IOError: - print(f"I/O error: {IOError}") + + + + + + + +#csv_columns = ['takhleuztoneudelam', 'data', 'sms', 'other', 'smscount', 'dataamount'] +#try: +# with open('export_vd_202109v1.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}")