There is already a text file of BOOTEX.doc (it is recommended to first convert the BOOTEX.doc
BOOTEX.docx
file into a txt file BOOTEX.txt ), please generate a two-dimensional list based on the text and print the output. The requirements are as follows:
(1) The three numbers in each row in the list correspond to the number of the three letters a, b, and c in the row (case distinction is made, capital letters are not counted)
(2) Output the list
def count_letters(line):
a_count = line.count('a') + line.count('A')
b_count = line.count('b') + line.count('B')
c_count = line.count('c') + line.count('C')
return [a_count, b_count, c_count]
def main():
file_name = "BOOTEX.txt" # 请确保BOOTEX.txt文件存在,并且是文本文件
result_list = []
with open(file_name, "r") as file:
for line in file:
counts = count_letters(line)
result_list.append(counts)
# 打印输出二维列表
for counts in result_list:
print(counts)
if __name__ == "__main__":
main()
PS: It is recommended to change the suffix name to txt first and put it in the same directory as the project file.