我想要的程序必须读取这样的文本文件:
EASTS versus WESTS
EASTS have scored 25:13
WESTS have scored 26:28
WESTS have scored 40:23
WESTS have scored 42:01
并运行以下输出:
WESTS 3
EASTS 1
我想我需要先把它分组。删除换行符。然后删除第一行中除大写字母之外的所有内容,并将它们分配给单独的变量。然后在文本中搜索这些变量出现的数量。所以这意味着 a = 2 和 b = 4 然后 - 每个总数减 1 并将其作为结果。这是我到目前为止所拥有的:
import string
teams = []
for word in open('commentary.txt'):
word = word[:-1] # gets away the /n characters.
word = word.strip("versus") # This line doesn't work
teams.append(word)
print(teams)
我想我知道该怎么做,但我不知道......任何帮助将不胜感激:D谢谢