This is my sample dataset in a CSV format:
Column[1], Column[2], Account, CostCentre, Rate, Ex VAT, VAT
000000000, 00000000, 4200213, G1023, 0, 10.50, 0.0
000000000, 00000000, 4200213, G1023, 20, 10.50, 2.1
000000000, 00000000, 4200213, G1023, 0, 10.50, 0.0
000000000, 00000000, 4200213, G1023, 20, 10.50, 2.1
I am trying to create an output file that focuses on the account number and further groups it by Cost Centre and Tax Rate, therefore, anything that has the account number 4200213 needs to be included in the output else all other rows can be ignored.
Secondly, if the Cost Centre is being repeated let's say in this instance G1023, I want the python script to establish whether the Tax Rates match and if so I want the output file to group it together by Rate and sum up the total Ex VAT and VAT cost in such a way that the desired result should be as follows:
Cost Centre, Rate, Ex VAT, VAT, In VAT
G1023, 0, 21, 0, 21
G1023, 20, 21, 4.2, 25.20
I've been trying to figure it out but without any success. My current code goes as follows:
import os
import sys
import csv
os.path.dirname = "./"
InputFile_name = "Book1.csv"
InputFile = csv.reader(open(InputFile_name, "r"))
OutputFile_name = "Journal.csv"
OutputFile = open(OutputFile_name, "w")
mydict = []
OutputFile.write("Cost Centre, Tax Rate, Total Ex VAT, VAT, Total In VAT\n")
for line in InputFile:
if line[2] == "4200213":
Cost_Centre = line[3]
Rate = line[4]
Ex_VAT = line[5]
VAT = line[6]
if Cost_Centre in mydict:
continue
else:
mydict.append(Cost_Centre)
for item in mydict:
if item in Cost_Centre and Rate == "0":
Ex_VAT += Ex_VAT
VAT+= VAT
In_VAT = Ex_VAT + VAT
elif item in Cost_Centre and Rate == "20":
Ex_VAT += Ex_VAT
VAT+= VAT
In_VAT = Ex_VAT + VAT
OutputFile.write(",".join([Cost_Centre,Rate,Ex_VAT,VAT,In_VAT+"\n"]))
OutputFile.close()
print "Finished."
sys.exit()
The script works but I am too far from getting the desired results. And as you'd have figured out that I am not very good at python so would appreciate if rather than just pointing out the mistake if you could make amendments on the script and provide me with the whole script please with some explanation as to what I am doing wrong.