python中csv文件中的某一列如何去停用词,还有如何进行中文分词
时间: 2023-06-06 15:06:21 浏览: 233
对于第一个问题,可以使用Python中的jieba库进行中文分词和去停用词操作。具体步骤如下:
1. 导入jieba库:
```python
import jieba
```
2. 定义停用词列表:
```python
stopwords = [line.strip() for line in open('stopwords.txt', encoding='utf-8').readlines()]
```
其中,stopwords.txt是一个包含停用词的文本文件,可以自己定义或者从网上下载。
3. 对每一行文本进行分词和去停用词:
```python
import csv
with open('data.csv', newline='', encoding='utf-8') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
words = [word for word in jieba.cut(row[0]) if word not in stopwords]
print(words)
```
其中,data.csv是一个包含待处理文本的CSV文件,第一列是待处理文本。
对于第二个问题,也可以使用jieba库进行中文分词。具体步骤如下:
1. 导入jieba库:
```python
import jieba
```
2. 对待分词的文本进行分词:
```python
text = '今天天气真好,适合出去玩。'
words = jieba.cut(text)
print(list(words))
```
输出结果为:
```
['今天天气', '真好', ',', '适合', '出去', '玩', '。']
```
如果需要获取每个词语的位置,可以使用jieba.lcut()函数:
```python
text = '今天天气真好,适合出去玩。'
words = jieba.lcut(text)
for i, word in enumerate(words):
print(i, word)
```
输出结果为:
```
0 今天天气
1 真好
2 ,
3 适合
4 出去
5 玩
6 。
```
阅读全文
相关推荐

















