Python 一个打开文本文件的程序,计算单词的数量并报告按它们在文件中出现的次数排序的前 N ​​个单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17493493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:20:20  来源:igfitidea点击:

A program that opens a text file, counts the number of words and reports the top N words ordered by the number of times they appear in the file?

pythonfileword-count

提问by kmz

Hi all im a beginner at programming, i was recently given the task of creating this program and i am finding it difficult. I have previously designed a program that calculates the number of words in a sentence that are typed in by the user, is it possible to modify this program to achieve what i want?

大家好,我是编程初学者,我最近接到了创建这个程序的任务,但我发现这很困难。我之前设计了一个程序来计算用户输入的句子中的单词数,是否可以修改这个程序来实现我想要的?

import string
def main():
  print "This program calculates the number of words in a sentence"
  print
  p = raw_input("Enter a sentence: ")
  words = string.split(p)
  wordCount = len(words)
  print "The total word count is:", wordCount
main()

采纳答案by Ashwini Chaudhary

Use collections.Counterfor counting words and open()for opening the file:

使用collections.Counter计数的单词和开放()打开文件:

from collections import Counter
def main():
    #use open() for opening file.
    #Always use `with` statement as it'll automatically close the file for you.
    with open(r'C:\Data\test.txt') as f:
        #create a list of all words fetched from the file using a list comprehension
        words = [word for line in f for word in line.split()]
        print "The total word count is:", len(words)
        #now use collections.Counter
        c = Counter(words)
        for word, count in c.most_common():
           print word, count
main()

collections.Counterexample:

collections.Counter例子:

>>> from collections import Counter
>>> c = Counter('aaaaabbbdddeeegggg')

Counter.most_commonreturns words in sorted order based on their count:

Counter.most_common根据字数按排序顺序返回字词:

>>> for word, count in c.most_common(): 
...     print word,count
...     
a 5
g 4
b 3
e 3
d 3

回答by jh314

To open files, you can use the openfunction

要打开文件,您可以使用open函数

from collections import Counter
with open('input.txt', 'r') as f:
    p = f.read() # p contains contents of entire file
    # logic to compute word counts follows here...

    words = p.split()

    wordCount = len(words)
    print "The total word count is:", wordCount

    # you want the top N words, so grab it as input
    N = int(raw_input("How many words do you want?"))

    c = Counter(words)
    for w, count in c.most_common(N):
       print w, count

回答by NIlesh Sharma

import re
from collections import Counter

with open('file_name.txt') as f:
    sentence = f.read()

words = re.findall(r'\w+', sentence)
word_counts = Counter(words)

回答by Injamul Islam

If anyone else is getting the error message for input, you may like to try this one,

如果其他人收到输入错误消息,您可能想尝试这个,

Code:

代码:

N = int(input("\nHow many words do you want: "))