# coding: utf8 import csv from datetime import datetime import sys from pprint import pprint FILE = 'VFCZ202109.CSV' #usrmonth = input("Month to be processed: ") #usrmonth = int(usrmonth) usrmonth = int(5) dictionary = {} def add_dict(id, key, value): dict = dictionary.get(id) value_ = float(value) if key in dict: dict[key] += value_ else: print("Never should happen", id) def add_dictsms(id, pricesms): dict = dictionary.get(id) print("increasing sms dict key for dict: ",dict," for :",pricesms) dict["smscount"] += pricesms #MAIN print("Start") with open(FILE, newline='', encoding="utf-8") as csvfile: reader = csv.DictReader(csvfile, delimiter=';',fieldnames=['faktura_c','phonenumber','direction','productgroup','productser','item','service','datetime','dialnum','time_taken','time_charged','dataamount','realcost','creditcost','discount' 'servicescost','chargedwovat','chargedwvat','privatecall']) for row in reader: print("Product serie: ",row['productser']) print("Time taken ",row['time_taken']) # sys.exit(1) #print(row) if row['phonenumber'] == '': continue #skip empty phonenumber # if row['Začátek fakturačního období'] == '': # continue # # my_string = row['Začátek fakturačního období'] # #my_string = '2021-09-06' # print("start date: ",my_string) # # try: # my_date = datetime.strptime(my_string, "%d.%m.%Y") # except: # print("An exception occurred, current my_string is: ", my_string) # sys.exit(1) #print(row['BILLED_PERIOD_START']) #print(my_date.month) # if usrmonth != my_date.month: # print("skipping bad date") # continue #skip bad dates MSISDN = int(row['phonenumber']) #ICCID = int(row['ICCID']) price = row['chargedwovat'].replace(',','.') #price = round(price, 5) typ = None if row['productser'] == "Používání služeb ": typ = 'data' elif row['productser'] == "SMS ": typ = "sms" else: print("groupvar: ",row['productser']) typ = 'other' if row['direction'] != "I" and row['productser'] == "SMS ": pricesms = int(row['time_taken']) print("sms price: ",pricesms,MSISDN) print("got some countsms") else: pricesms = 0 #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} if price != '': print("Editing dictionary: ", MSISDN, "typ: ", typ, "cena: ", price) add_dict(MSISDN, typ, price) if row['direction'] != "I" and row['productser'] == "SMS ": print("pricesms: ", pricesms, " for unit: ", dictionary[MSISDN]) add_dictsms(MSISDN, pricesms) else: print("direction: ", row['direction'], " and productser: ", row['productser']) #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('export_vd202109v1.csv', 'w') as f: for key in dictionary.keys(): f.write("%s,%s\n"%(key,dictionary[key])) 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) except IOError: print(f"I/O error: {IOError}")