繁体   English   中英

Python - 计算文本文件中的单词

[英]Python - Counting Words In A Text File

我是Python的新手,正在开发一个程序,它将计算简单文本文件中的单词实例。 程序和文本文件将从命令行中读取,因此我已将其包含在我的编程语法中以检查命令行参数。 代码如下

import sys

count={}

with open(sys.argv[1],'r') as f:
    for line in f:
        for word in line.split():
            if word not in count:
                count[word] = 1
            else:
                count[word] += 1

print(word,count[word])

file.close()

count是一个字典,用于存储单词及其出现次数。 我希望能够打印出每个单词及其出现的次数,从大多数事件开始到最少出现。

我想知道我是否在正确的轨道上,如果我正确使用系统。 谢谢!!

你做的对我来说很好,也可以使用collections.Counter (假设你是python 2.7或更新版本)来获取更多的信息,比如每个单词的数量。 我的解决方案看起来像这样,可能会有一些改进。

import sys
from collections import Counter
lines = open(sys.argv[1], 'r').readlines()
c = Counter()
for line in lines:
    for work in line.strip().split():
        c.update(work)
for ind in c:
    print ind, c[ind]

您的最终print没有循环,因此它只会打印您读取的最后一个单词的计数,这仍然是word的值。

此外,使用with context manager,您不需要close()文件句柄。

最后,正如评论中指出的那样,您需要在split之前从每line删除最终换行符。

对于像这样的简单程序,它可能不值得麻烦,但您可能希望查看Collections中的defaultdict以避免在字典中初始化新键的特殊情况。

我刚刚注意到一个拼写错误:你打开文件为f但你把它关闭为file 正如tripleee所说,您不应该关闭在with语句中打开的文件。 此外,使用内置函数的名称(如filelist )作为您自己的标识符也是不好的做法。 有时它有效,但有时它会导致讨厌的错误。 对于阅读代码的人来说,这让人感到困惑; 语法高亮编辑器可以帮助避免这个小问题。

要按照count的降序打印count字典中的数据,您可以执行以下操作:

items = count.items()
items.sort(key=lambda (k,v): v, reverse=True)
print '\n'.join('%s: %d' % (k, v) for k,v in items)

有关list.sort()方法和其他方便的dict方法的更多详细信息,请参阅Python Library Reference。

我只是通过使用re库来做到这一点。 这是每行文本文件中的平均单词,但您必须找出每行的单词数。

import re
#this program get the average number of words per line
def main():
    try:
        #get name of file
        filename=input('Enter a filename:')

        #open the file
        infile=open(filename,'r')

        #read file contents
        contents=infile.read()
        line = len(re.findall(r'\n', contents))
        count = len(re.findall(r'\w+', contents))
        average = count // line

        #display fie contents
        print(contents)
        print('there is an average of', average, 'words per sentence')

        #closse the file
        infile.close()
    except IOError:
        print('An error oocurred when trying to read ')
        print('the file',filename )

#call main
main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM