修改脚本让分词后的结果保存在第二列中import jieba import csv # 加载停用词表 stopwords = set() with open('stopwords.txt', 'r', encoding='utf-8') as f: for line in f: stopwords.add(line.strip()) # 读取文件内容 file_object2 = open('test.csv').read().split('\n') # 分词并去除停用词 Rs2 = [] for i in range(len(file_object2)): result = [] seg_list = jieba.cut(file_object2[i]) for w in seg_list: if w not in stopwords: # 如果不是停用词,则将其添加到结果列表中 result.append(w) Rs2.append(result) # 写入CSV文件 with open('processed_data.csv', 'w', encoding='utf-8', newline='') as file: writer = csv.writer(file) writer.writerows(Rs2)
时间: 2024-03-01 19:55:33 浏览: 20
你可以将修改后的代码如下:
```python
import jieba
import csv
# 加载停用词表
stopwords = set()
with open('stopwords.txt', 'r', encoding='utf-8') as f:
for line in f:
stopwords.add(line.strip())
# 读取文件内容
file_object2 = open('test.csv').read().split('\n')
# 分词并去除停用词
Rs2 = []
for i in range(len(file_object2)):
result = []
seg_list = jieba.cut(file_object2[i])
for w in seg_list:
if w not in stopwords: # 如果不是停用词,则将其添加到结果列表中
result.append(w)
Rs2.append(result)
# 写入CSV文件
with open('processed_data.csv', 'w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
for i in range(len(Rs2)):
writer.writerow(['', ' '.join(Rs2[i])]) # 将分词结果保存在第二列中,第一列为空
```
在上面的代码中,我们将分词结果保存在了`Rs2`列表中,并且将分词结果写入了`processed_data.csv`文件中。我们通过`writer.writerow(['', ' '.join(Rs2[i])])`来将分词结果保存在第二列中,第一列为空。