python代码不读取文件中的第一行

时间:2017-10-14 14:50:08

标签: python python-3.x

所以我有这段代码应该遍历文件中的每一行,然后将每一行添加到列表中,然后删除该列表(去除空格和\ n),然后最后附加那些将项目列入一个大清单。此列表包含文件中每行的每个句子中的每个单词。 我在这里的代码确实完全除了一个细节,它出于某种原因跳过第一行。

def counter(words):
    frequency = {}
    for word in words:
        if word not in frequency:
            frequency[word] = 1
        elif word in frequency:
            frequency[word] += 1
    return frequency


def main():
    print("This program shows the frequency of words in a file.\n"
          "Could you please enter the file name, without extension?")
    file_name = input('') + '.txt'
    with open(file_name, "r") as word_file:
        words = []
        for lines in word_file:
            for line in lines:
                line = word_file.readline()
                temp_words = line.split()
                print(temp_words)
                for word in temp_words:
                    words.append(word)
    print(counter(words))

这是整个代码,但你们只需要专注于主要功能,谢谢你!

1 个答案:

答案 0 :(得分:4)

有一些冗余:

for lines in word_file:  # this will move the iterator one forward
  for line in lines: # this actually iterates through the chars in the line
    line = word_file.readline()  # but this moves the iterator ahead, too

以下内容足以替换for循环:

for line in word_file:
  words.extend(line.split())

顺便说一下,你的核心程序可以写成:

from collections import Counter
with open(file_name, "r") as word_file:
  c = Counter(word for line in word_file for word in line.split())
print(c)