读txt文件报错_CodingPark编程公园
发布日期:2021-06-29 15:46:49 浏览次数:2 分类:技术文章

本文共 650 字,大约阅读时间需要 2 分钟。

报错

’utf-8’ codec can’t decode byte 0xb6 in position 2

在这里插入图片描述

解决办法

1 ✅

file = open('/Users/atom-g/Desktop/DanMuAnalyzePark/FuDanUniversity_data/test_corpus/corpus/1.txt', 'r', encoding='gbk')

在这里插入图片描述

2-代码美观一点 ✅

from pyhanlp import *def readtxt(path):    with open(path, 'r', encoding='gbk') as fr:        content = fr.read()        return contenttext = readtxt('/Users/atom-g/Desktop/DanMuAnalyzePark/FuDanUniversity_data/test_corpus/corpus/1.txt')text_process = HanLP.segment(text)text_list = [(str(i.word), str(i.nature)) for i in text_process]# print(text_list)words = []for i in text_list:    if i[1] != 'w' and len(i[0])>1:        words.append(i[0])print(words)

在这里插入图片描述

转载地址:https://codingpark.blog.csdn.net/article/details/106929448 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:文件读&写_Python_CodingPark编程公园
下一篇:使用AWS Educate Starter Account 创建博客_CodingPark编程公园

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月02日 21时57分49秒