为什么 readline() 没有从我的 .txt 中获取所有单词

Why is readline() not getting all words from my .txt

我正在打开一个文本文件,逐行读取它并去掉尾随的 '\n' 并将其附加到一个列表中,代码如下。这样做时有些词会丢失。谁能告诉我为什么?

def compare(t, w):
    c = {}
    m = []
    for line in t:
        lines = t.readline()
        word = lines.strip()
        m.append(word)
    for x in m:
        c[x] = c.get('x', 0)
    if w in c:
        print('True')
    else:
        print('False')


fin = open('words.txt')

compare(fin, 'expect')

不要在 for line in t 循环中使用 .readline。 每次迭代都会读取一个新行,并且在 .readline 中将读取下一行,因此每次迭代都会跳过一行。