99 lines
2.5 KiB
Python
99 lines
2.5 KiB
Python
import csv
|
|
from datetime import datetime
|
|
import sys
|
|
|
|
FILE = 'TMGDSP202110.CSV'
|
|
|
|
usrmonth = input("Month to be processed: ")
|
|
|
|
|
|
|
|
dictionary = {}
|
|
|
|
def add_dict(id, key, value):
|
|
dict = dictionary.get(id)
|
|
if key in dict:
|
|
dict[key] += value
|
|
else:
|
|
print("Never should happen", id)
|
|
|
|
|
|
|
|
#MAIN
|
|
with open(FILE, newline='', encoding='utf-8') as csvfile:
|
|
reader = csv.DictReader(csvfile, delimiter=';')
|
|
for row in reader:
|
|
|
|
#print(row)
|
|
if row['MSISDN'] == '':
|
|
continue #skip empty MSISDN
|
|
#print(row)
|
|
if row['ICCID'] == '':
|
|
continue #skip empty ICCID
|
|
|
|
if row['BILLED_PERIOD_START'] == '':
|
|
continue
|
|
|
|
my_string = row['BILLED_PERIOD_START']
|
|
#my_string = '2021-09-06'
|
|
print(my_string)
|
|
try:
|
|
my_date = datetime.strptime(my_string, "%Y-%m-%d")
|
|
except:
|
|
print("An exception occurred, current my_string is: ", my_string)
|
|
sys.exit(1)
|
|
|
|
#print(row['BILLED_PERIOD_START'])
|
|
#print(my_date.month)
|
|
|
|
|
|
|
|
if my_date.month != '5':
|
|
continue #skip bad dates
|
|
|
|
|
|
|
|
|
|
MSISDN = int(row['MSISDN'])
|
|
ICCID = int(row['ICCID'])
|
|
price = float(row['CHARGE_NET'].replace(',','.'))
|
|
|
|
typ = None
|
|
if row['CHARGE_TYPE'] == "Pravidelné měsíční poplatky":
|
|
typ = "data"
|
|
elif row['CHARGE_TYPE'] == "Poplatky za roamingový datový provoz":
|
|
typ = "data"
|
|
elif row['CHARGE_TYPE'] == "Poplatky za národní datový provoz":
|
|
typ = "data"
|
|
elif row['CHARGE_TYPE'] == "Poplatky za roamingový SMS provoz":
|
|
typ = "sms"
|
|
elif row['CHARGE_TYPE'] == "Poplatky za národní SMS provoz":
|
|
typ = "sms"
|
|
else:
|
|
typ = 'other'
|
|
|
|
|
|
#only if not exist create new record in dictionary for ID
|
|
if not MSISDN in dictionary:
|
|
dictionary[MSISDN] = {'ICCID':ICCID,'data':0.0, 'sms':0.0, 'other':0.0}
|
|
|
|
add_dict(MSISDN, typ, price)
|
|
|
|
|
|
#try:
|
|
#print(dictionary[436761890319959]) #one with both types
|
|
#except:
|
|
#print("Required unit is not in our set")
|
|
#sys.exit(1)
|
|
|
|
print(len(dictionary.keys()))
|
|
for key, value in dictionary.items():
|
|
print(key, ' : ', value)
|
|
|
|
|
|
print("Count of units: ", len(dictionary.keys()))
|
|
|
|
with open('TMGDSP_export_202110.csv', 'w') as f:
|
|
for key in dictionary.keys():
|
|
f.write("%s,%s\n"%(key,dictionary[key]))
|