如何计算.txt文件中有多少个单词

2024-10-02 04:29:11 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我知道如何计算字符串(o = len(x.split()) print(o))中有多少个单词,但如何计算.txt文件中有多少个单词

顺便说一句,我的代码如下所示:

p = open("einsteinsbiography", "r", encoding="utf8")
x = dict()
for line in p:
    line = line.strip()
    line = line.lower()
    t = line.split(" ")
    for word in t:
        if word in x:
            x[word] = x[word] + 1
        else:
            x[word] = 1
for key in list(x.keys()):
    print(key, ":", x[key])

    

Tags: 文件key字符串代码intxtforlen
3条回答

内存友好的细微变化(它逐行迭代.txt文件,而不是一次加载所有文件):

with open("einsteinsbiography", "r", encoding="utf-8") as file:
    total = 0
    for line in file:
        # Remove all kinds of trailing whitespace with rstrip method
        total += len((line.rstrip()).split(' '))
print(total)

如果你想计算每个单词的实例数,我推荐big_bad_bison的答案,并使用计数器对象

文本文件中的字数

num_chars = sum(len(word) for word in open('names.txt').read().split())
print(num_chars)

要仅获取文件中的总字数,请执行以下操作:

with open("einsteinsbiography", "r", encoding="utf8") as p:
    txt = p.read()
words = txt.split()
print(len(words))

如果要获取包含文件中每个单词计数的dict,请使用Counter

from collections import Counter

with open("einsteinsbiography", "r", encoding="utf8") as p:
    txt = p.read()
words = txt.split()
c = Counter(words)
print(c)

相关问题 更多 >

    热门问题