加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何从文件中打印特定行

发布时间:2020-12-20 12:10:28 所属栏目:Python 来源:网络整理
导读:我正在尝试从“Scores”文件中打印一个特定的行,这是选项B.这是我的代码: print("Option A: Show all scoresnOption B: Show a recordnOption Q: Quit")decision = input("Enter A,B,C or Q: ")myFile = open("Scores.txt","rt")if decision == "A": reco
我正在尝试从“Scores”文件中打印一个特定的行,这是选项B.这是我的代码:

print("Option A: Show all scoresnOption B: Show a recordnOption 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,然后我输入了一个持有玩家得分的玩家名称,但它会抛出此错误消息:

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()

希望它有效!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读