I need to write a Python program to read/close the file (i.e., Stock.txt), and display the following output, using the split method of list. There is only one line in the Stock.txt which is the stock portfolio of an investor, consisting of the invested amount of four stocks.
Inside content of the file Stock.txt:
hsbc, 84564.24, boc, 46392.45, manulife, 34562.98, galaxy, 89321.23
I only know how to write related Python code to open to read/close the file. I really don't know what python code I should write to display the following expected output which the assignment requires me to do!
My current code:
infile = open("Stock.txt", 'c')
data = [line.rstrip() for line in infile]
infile.close()
But I am not sure whether my current code is right since I am a Python beginner.
Expected output of this assignment:
01234567890123456789012345678901234567890123456789
The amount invested in HSBC: 844563.24
The amount invested in BOC: 465392.46
The amount invested in MANULIFE: 345612.98
The amount invested in GALAXY: 893421.23
STOCK PERCENTAGE
---------------------
HSBC 33.13%
BOC 18.26%
MANULIFE 13.56%
GALAXY 35.05%
Total Amount Invested: $2,548,989.91