这是一个分词代码 请问一下为什么运行老是出现以下问题
import os
import jieba
import jieba.posseg as psg
import re
import pandas as pd
def get_stop_dict(file):
content = open(file, encoding="utf-8")
word_list = []
for c in content:
c = re.sub('\n|\r', '', c)
word_list.append(c)
return word_list
file_path = input("请输入当前文件夹路径:")
os.chdir(file_path)
stop_file = input("请输入停用词文件名字:")
user_file = input("请输入用户词典文件名字:")
##stop_file = "stopwordlist.txt"
##user_file = "add_word_list.txt"
stop_words = get_stop_dict(stop_file)
file_name = input("请输入文件名字:")
text = open(file_name, encoding="utf-8").read()
jieba.load_userdict(user_file)
text_lines = text.split('\n')
flag_list = ['n', 'nz', 'vn']
counts = {}
for line in text_lines:
line_seg = psg.cut(line)
for word_flag in line_seg:
word = re.sub("[^\u4e00-\u9fa5]", "", word_flag.word)
if word_flag.flag in flag_list and len(word) > 1 and word not in stop_words:
counts[word] = counts.get(word, 0) + 1
word_freq = pd.DataFrame({'word': list(counts.keys()), 'freq': list(counts.values())})
word_freq = word_freq.sort_values(by='freq', ascending=False)
word_freq.to_excel("word_freq.xlsx", index=False)
input("Press <enter>")
运行之后是这样的:
D:\python\python基础第一章\venv\Scripts\python.exe D:/新建文件夹/分词代码.py
请输入当前文件夹路径:D:\新建文件夹
请输入停用词文件名字:stopwordlist.txt
请输入用户词典文件名字:add.txt
请输入文件名字:text.txt
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\Ace\AppData\Local\Temp\jieba.cache
Loading model cost 0.415 seconds.
Prefix dict has been built successfully.
Traceback (most recent call last):
File "D:\新建文件夹\分词代码.py", line 42, in <module>
word_freq.to_excel("word_freq.xlsx", index=False)
File "D:\python\python基础第一章\venv\lib\site-packages\pandas\core\generic.py", line 2345, in to_excel
formatter.write(
File "D:\python\python基础第一章\venv\lib\site-packages\pandas\io\formats\excel.py", line 888, in write
writer = ExcelWriter( # type: ignore[abstract]
File "D:\python\python基础第一章\venv\lib\site-packages\pandas\io\excel\_openpyxl.py", line 49, in __init__
from openpyxl.workbook import Workbook
ModuleNotFoundError: No module named 'openpyxl'
Process finished with exit code 1