如何从文件中打印特定行 [英] How to print a specific line from a file
本文介绍了如何从文件中打印特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从文件分数"(即选项B)中打印一行.这是我的代码:
I am trying to print a specific line from the file "Scores", which is option B. This is my code:
print("Option A: Show all scores\nOption B: Show a record\nOption Q: Quit")
decision = input("Enter A, B, C or Q: ")
myFile = open("Scores.txt", "rt")
if decision == "A":
record = myFile.read()
print(record)
myFile.close()
elif decision == "B" or decision == "b":
playerName = input("Enter a player name to view their scores: ")
record = myFile.read()
answer = record.find(playerName)
for line in answer:
print(line)
elif decision == "Q" or decision == "q":
exit
我选择了选项B,然后输入了保留该球员得分的球员名称,但是会抛出以下错误消息:
I went for Option B, then I entered a player name that holds the score of the player, but it throws this error message:
line 12, in <module>
for line in answer():
TypeError: 'int' object is not callable
推荐答案
从我这边走了几分钱:
file = open("file")
lines = file.readlines()
for line in lines:
if playername in line:
print line
file.close()
希望它能起作用!
这篇关于如何从文件中打印特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文