In: Computer Science
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)
in python
''' open file and paremeter is given to it is path+file name '''
f = open(r"D:\vscode\python programe\test.txt",'r')
dict={}
for x in f:
# print(x)
row=x.split()
'''
each company name is converted into upper case. And make company name is key.
And the value of the key is (payment in - payment out) which is 2nd and 3rd columns respectively in file .
'''
dict[row[0].upper()]=int(row[1])-int(row[2])
for key,value in dict.items():
print(key,": ",value) # printing dictonary
First, open the file using the "open()"function and give the
path of the file as a parameter.and store file content in "f"
variable.
after that read " f " line by line as a file.
Create a dictionary and store data f[0] contain File name , F[1] is
payment_in and f[2] is payment_out.
make f[0] as key and f[1]-f[2] is value of the key.
Note:
For running in your computer you have to change the path of the file where your file is located. and my file name is test.txt.
f = open(r"your file path\file name",'r')
my demo file:
test.txt
company_A 1000 900
company_B 800 1100
company_C 1200 1400
company_D 1500 1000
company_E 1250 1000
company_F 1400 1500
company_G 1500 1400
company_H 1100 1300