找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1407|回复: 0

[代码与实例] 求助,如何统计word文档高频词汇

1

主题

1

帖子

1

积分

贫民

积分
1
小西xiaoxi1 发表于 2021-9-15 16:46:00 | 显示全部楼层 |阅读模式
# -*- coding: utf-8 -*-
import jieba

txt = open("小说.txt","r",encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}

for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word]=counts.get(word,0)+1
        
items = list(counts.items())
items.sort(key=lambda x: x[1],reverse=true)

for i in range(3):
    word,count=items[i]
    print("[{0:<5}{1:5}".format(word,count))



我想统计文档里面出现的前50个高频词汇,这是我从网上复制的代码,但是会出现以下提示错误

Traceback (most recent call last):
  File "F:/1/个人学习/python/统计频率.py", line 4, in <module>
    txt = open("小说.txt","r",encoding='utf-8').read()
  File "D:\lib\codecs.py", line 322, in decode
    (result, c**umed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte

请问改如何修改?感谢大家


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表