In: Computer Science
#This is what you have to do: #1.Open the testFile.csv #2.Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives as well. # Hint: Absolute value could be derived by using abs( number ) #2.d. Your net balance is sum(payments in - payments out) #3. Print resulting dictionary #4. Upload the printed result along with this file
TEST FILE INFO
Counterparty | Amount Received | Amount Paid |
Utes | 0 | 1100070 |
Vlas Fancy LLC | 0 | -5699995 |
Thurinus Adoptations | 0 | 3471334 |
Utes | 0 | 8146767 |
Limited Limits LLC | 4744912 | 0 |
Vlas Fancy LLC | 0 | 2936543 |
LLC Utes | 4170585 | 0 |
Caesarian Trust | 7681654 | 0 |
Vlas Fancy LLC | 0 | 7550468 |
Salv CLL | 0 | 3625075 |
Utes LLC | 0 | -6601733 |
# dictionary for holding the data,Utes is initialised to 0 since
# its in multiple names and we have to add its net balance to current one
companies = {'Utes':0}
#1.Open the testFile.csv
with open('testFile.csv') as csvfile:
# for first line/header detection
i = 1
# looping through each line in the csvfile
for row in csvfile:
# if its first line/header skip to next
if i == 1:
i = 2
continue
# removing \n character in the end
row = row.replace('\n','')
# first element when row splited with '\t' will be Counterparty [since \t is the delemiter]
Counterparty = row.split('\t')[0]
# second element when row splited with '\t' will be AmountReceived [since \t is the delemiter]
AmountReceived = row.split('\t')[1]
# third element when row splited with '\t' will be AmountPaid [since \t is the delemiter]
AmountPaid = row.split('\t')[2]
# calculating net balance as sum of amount in - amount out
NetBalance = int(AmountReceived)-abs(int(AmountPaid))
# checking if Utes is in Counterparty
if 'Utes' in Counterparty:
# if true then add NetBalance to current Utes NetBalance,Thats why Utes is initialised with 0
companies['Utes'] += NetBalance
# checking for existing Counterparty
elif Counterparty in list(companies.keys()):
companies[Counterparty] += NetBalance
else:
# else set Counterparty as the key and NetBalance as the value
companies[Counterparty] = NetBalance
# printing the final dictionary
print(companies)
testFile.csv
Code Screenshot
Output-:
PS-:If you have any doubts/problems please comment below