【问题标题】:why doesn't this work even though the strings match [closed]为什么即使字符串匹配也不起作用[关闭]
【发布时间】:2018-11-14 17:48:21
【问题描述】:
games=[]

file=open("egames.txt",'r')
for game in file:
    games.append(game)
file.close()

print("All games made by Rockstar Games")
for game in games:
    currentline=game.split(",")
    publisher=currentline[5]
    if publisher=="Rockstar Games":
        print(currentline[0],currentline[1])

我没有收到任何错误,我只是没有打印任何内容] 使用 Rockstar Games。The actual Text file

【问题讨论】:

标签: python file filereader


【解决方案1】:

从文件迭代器中读取的行以换行符结尾。您应该将它们作为规范化的一部分剥离:

for game in file:
    games.append(game.rstrip())

【讨论】:

  • 很高兴能提供帮助。如果您认为此答案正确,您能否将其标记为已接受? (点击答案旁边的灰色复选标记。)
【解决方案2】:

我猜问题出在尾随的换行符上,这些字符对您的眼睛是不可见的。尝试去除任何空白:

publisher = currentline[5].strip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多