2017-10-14 52 views
0

因此,我有這段代碼應該遍歷文件中的每一行,然後將每行添加到列表中,然後去掉該列表(以去掉空格和\ n's),然後最後將這些列表項添加到一個大列表中。該列表包含文件中每行的每個句子的每個單詞。 我在這裏的代碼完全除了一個細節之外,它由於某種原因跳過第一行。python code not reading first line in file

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)) 

這是整個代碼,但你們只需要關注主要功能,謝謝!

回答

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) 
+0

謝謝你這個人。 我仍然是python的noob; p (我會盡快給你答案一個複選標記,需要再等10分鐘) – GotYa