pandas导入时utf-8编码报错 ,使用gbk编码后在分词的时候报错,这可咋整?

如果使用encoding=utf-8,报错如下

img


将导入时encoding改成GBK
split的时候报错

img

stop=[]
text=[]
after_text=[]
file_stop=r"D:\stopword(停用词).txt"
file_text=r"D:/简历表.csv"
with open(file_stop, "r", encoding="GBK") as f:
    for line in f.readlines():
        lline=line.strip()
        stop.append(lline)
with open(file_text, "r", encoding="GBK") as f:
    print(f.readlines())
    for line in f.readlines():
        lline = line.split()
        for i in lline :
            if i not in stop:
                after_text.append(i)
print(after_text)
with open(r"D:\新桌面\前端\训练数据\训练数据\stop.txt","w+") as f:
    for i in after_text:
        f.write(i)


用pandas 自带的读取函数,里面有属性调整读取格式,可以网上查一下

解决了。pandas导入的时候encoding="utf-8"报错时,可以先open文件再read


df=read_csv(open"文件路径",encoding="utf-8")