【发布时间】:2020-01-22 13:20:02
【问题描述】:
我需要编写一个 Python 程序来读取/关闭文件(即 Stock.txt),并使用 list 的 split 方法显示以下输出。 Stock.txt中只有一行是投资者的股票投资组合,由四只股票的投资金额组成。
Stock.txt 文件的内部内容:
hsbc, 84564.24, boc, 46392.45, manulife, 34562.98, galaxy, 89321.23
我只知道如何编写相关的 Python 代码来打开以读取/关闭文件。我真的不知道我应该编写什么 Python 代码来显示作业要求我执行的以下预期输出!
我当前的代码:
infile = open("Stock.txt", 'c')
data = [line.rstrip() for line in infile]
infile.close()
但我不确定我当前的代码是否正确,因为我是 Python 初学者。
此作业的预期输出:
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
【问题讨论】:
标签: python python-3.x list