用python实现txt文件词频统计
时间: 2023-09-16 08:14:23 浏览: 132
好的,以下是用Python实现txt文件词频统计的代码:
```python
import string
# 定义一个函数用来读取txt文件并返回一个字符串
def read_file(filename):
with open(filename, 'r', encoding='utf-8') as f:
text = f.read()
return text
# 定义一个函数用来统计单词出现次数
def count_words(text):
# 将所有单词转换为小写并去除标点符号
text = text.lower().translate(str.maketrans('', '', string.punctuation))
# 将字符串分割成单词列表
words = text.split()
# 统计每个单词出现的次数
word_counts = {}
for word in words:
if word not in word_counts:
word_counts[word] = 1
else:
word_counts[word] += 1
return word_counts
# 调用函数读取文件并统计单词出现次数
text = read_file('example.txt')
word_counts = count_words(text)
# 打印出现次数最高的前10个单词
sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
for i in range(10):
print(sorted_word_counts[i])
```
注意,这里需要先定义两个函数,一个用来读取txt文件并返回一个字符串,另一个用来统计单词出现次数。代码中使用了Python的标准库string来去除标点符号,使用了Python的字典来统计单词出现次数,并使用了sorted函数来按照出现次数从高到低排序单词。
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)