import wordcloud # 读取文本 with open("./数据集/desc.txt",encoding="utf-8") as f: s = f.read() wc=wordcloud.WordCloud(width=1000,height=700,background_color='white',max_words=100) wc.generate(s) # 加载词云文本 wc.to_file("word_cloud_result.png") # 保存词云文件
时间: 2024-03-09 18:47:03 浏览: 76
这是一个使用`wordcloud`生成词云并保存词云文件的例子。代码中会读取名为`desc.txt`的文本文件,生成包含100个词的词云,并将结果保存为PNG格式的图片文件`word_cloud_result.png`。您可以根据需要修改相应的参数和文件路径。
```python
import wordcloud
# 读取文本文件
with open("./数据集/desc.txt", encoding="utf-8") as f:
s = f.read()
# 创建词云对象
wc = wordcloud.WordCloud(width=1000, height=700, background_color='white', max_words=100)
# 加载词云文本
wc.generate(s)
# 保存词云文件
wc.to_file("word_cloud_result.png")
```
需要注意的是,这个例子中使用了相对路径`./数据集/desc.txt`,您需要根据实际情况修改路径。同时,如果您的文本文件中包含了中文字符,需要指定相应的编码格式。
相关问题
pandas版本是2.0.1 from bs4 import BeautifulSoup import pandas as pd import requests import time df = pd.read_csv('playlist.csv', header=None,error_bad_lines=False, names=['url', 'title', 'play', 'user']) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' } for i in df['url']: time.sleep(2) url = 'https://music.163.com' + i response = requests.get(url=url, headers=headers) html = response.text soup = BeautifulSoup(html, 'html.parser') # 获取歌单标题 title = soup.select('h2')[0].get_text().replace(',', ',') # 获取标签 tags = [] tags_message = soup.select('.u-tag i') for p in tags_message: tags.append(p.get_text()) # 对标签进行格式化 if len(tags) > 1: tag = '-'.join(tags) else: tag = tags[0] # 获取歌单介绍 if soup.select('#album-desc-more'): text = soup.select('#album-desc-more')[0].get_text().replace('\n', '').replace(',', ',') else: text = '无' # 获取歌单收藏量 collection = soup.select('#content-operation i')[1].get_text().replace('(', '').replace(')', '') # 歌单播放量 play = soup.select('.s-fc6')[0].get_text() # 歌单内歌曲数 songs = soup.select('#playlist-track-count')[0].get_text() # 歌单评论数 comments = soup.select('#cnt_comment_count')[0].get_text() # 输出歌单详情页信息 print(title, tag, text, collection, play, songs, comments) # 将详情页信息写入CSV文件中 with open('music_message.csv', 'a+', encoding='utf-8-sig') as f: f.write(title + ',' + tag + ',' + text + ',' + collection + ',' + play + ',' + songs + ',' + comments + '\n') # 获取歌单内歌曲名称 li = soup.select('.f-hide li a') for j in li: with open('music_name.csv', 'a+', encoding='utf-8-sig') as f: f.write(j.get_text() + '\n')
根据你提供的代码,似乎是在使用 pandas 读取一个名为 'playlist.csv' 的文件,并对文件中的每个 URL 访问网页获取歌单信息和歌曲名称,并将这些信息写入两个不同的 CSV 文件中。在读取 CSV 文件时,你使用了 `error_bad_lines=False` 来跳过读取失败的行。但是,根据你提供的 pandas 版本号是 2.0.1,这个版本并不支持 `error_bad_lines` 这个参数,因此会导致出现错误提示。你可以尝试升级 pandas 版本,或者直接删掉这个参数。另外,你的代码中没有明显的错误,但建议在向 CSV 文件写入数据时,最好使用 pandas 的 `to_csv()` 函数,这样可以更方便地进行数据写入和数据处理。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)