为什么即使字符串匹配也无法正常工作

时间:2018-11-14 17:48:21

标签: python file filereader

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

我猜测问题是尾随换行符,您看不到它们。尝试剥离任何空白:

publisher = currentline[5].strip()