用jieba库对文档 nove.txt 的文本内容进行分词,词频统计的结果排序输出到 result.csv 中
时间: 2024-02-27 12:56:29 浏览: 44
好的,下面是针对您的要求的代码示例:
```python
import jieba
from collections import Counter
import csv
# 读取文本文件
with open("nove.txt", "r", encoding="utf-8") as f:
text = f.read()
# 使用jieba分词
words = jieba.cut(text)
# 统计词频
word_count = Counter(words)
# 将词频统计结果排序,并写入csv文件中
with open("result.csv", "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow(["word", "count"])
for word, count in word_count.most_common():
writer.writerow([word, count])
```
执行完上述代码后,会在当前目录下生成一个名为"result.csv"的文件,文件中包含了对文档"nove.txt"进行分词后的词频统计结果,按照词频从高到低排序。
相关问题
使用python爬取微博近两天的前十个热搜,写入文档nove.txt,使用jieba进行词频统计,结果排序输出到result.csv中,最后生成词云图。
好的,这是一个比较复杂的任务,但是可以分步骤来完成。首先,我们需要使用Python爬取微博热搜榜单上的热门关键词。可以使用`requests`和`BeautifulSoup`库来完成这个任务。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://s.weibo.com/top/summary'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
keywords = soup.select('.td-02 a')
with open('nove.txt', 'w', encoding='utf-8') as f:
for i in range(10):
keyword = keywords[i].text.strip()
f.write(keyword + '\n')
```
这段代码会将前十个热门关键词写入到`nove.txt`文件中。
接下来,我们需要使用`jieba`库进行分词和词频统计。以下是示例代码:
```python
import jieba
import csv
jieba.setLogLevel(20)
jieba.load_userdict('userdict.txt')
with open('nove.txt', 'r', encoding='utf-8') as f:
text = f.read()
words = jieba.lcut(text)
word_counts = {}
for word in words:
if len(word) == 1:
continue
elif word == '微博':
continue
else:
word_counts[word] = word_counts.get(word, 0) + 1
word_counts_items = list(word_counts.items())
word_counts_items.sort(key=lambda x: x[1], reverse=True)
with open('result.csv', 'w', encoding='utf-8-sig', newline='') as f:
writer = csv.writer(f)
writer.writerow(['词语', '词频'])
for item in word_counts_items:
writer.writerow([item[0], item[1]])
```
这段代码会将`nove.txt`中的文本进行分词和词频统计,并将结果按照词频排序写入到`result.csv`文件中。
最后,我们可以使用`wordcloud`库生成词云图。以下是示例代码:
```python
import wordcloud
with open('nove.txt', 'r', encoding='utf-8') as f:
text = f.read()
wc = wordcloud.WordCloud(
background_color='white',
font_path='C:/Windows/Fonts/simhei.ttf',
max_words=100,
width=800,
height=600,
margin=2
)
wc.generate(text)
wc.to_file('wordcloud.png')
```
这段代码会将`nove.txt`中的文本生成词云图,并将结果保存到`wordcloud.png`文件中。
以上就是完整的代码示例,可以将这些代码组合起来,完成任务。
如何使用Python中的jieba库对分词后的文本进行词频统计
使用jieba库对文本进行分词,可以得到一个词语列表,然后使用Python中的collections库中的Counter类对词语列表进行词频统计。具体步骤如下:
1. 安装jieba库:`pip install jieba`
2. 导入jieba库和collections库中的Counter类:
```
import jieba
from collections import Counter
```
3. 对文本进行分词:
```
text = '我喜欢吃苹果,也喜欢吃香蕉'
word_list = jieba.cut(text) # jieba分词,返回一个生成器对象
```
4. 对词语列表进行词频统计:
```
word_count = Counter(word_list) # 统计词频
```
5. 输出词频结果:
```
print(word_count)
```
完整代码如下:
```
import jieba
from collections import Counter
text = '我喜欢吃苹果,也喜欢吃香蕉'
word_list = jieba.cut(text)
word_count = Counter(word_list)
print(word_count)
```
输出结果为:
```
Counter({'喜欢': 2, '吃': 2, '我': 1, '苹果': 1, ',': 1, '也': 1, '香蕉': 1})
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)