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))
這是整個代碼,但你們只需要關注主要功能,謝謝!
謝謝你這個人。 我仍然是python的noob; p (我會盡快給你答案一個複選標記,需要再等10分鐘) – GotYa