python读入中文文本编码错误
2016-12-06 21:02
218 查看
python读入中文文本编码错误
python读入中文txt文本:
但是有时候会出现错误提示:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 10: illegal multibyte sequence
此时,需要对代码做一个小的调整,就可以读入中文,即以中文二进制'rb'读入txt,然后转换为'utf-8',具体代码如下:
python读入中文txt文本:
#coding:utf-8 def readFile(): fp = open('emotion_dict//neg//neg_all_dict.txt','r') list = [] for line in fp: list.append(line) fp.close() print(list) readFile()
但是有时候会出现错误提示:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 10: illegal multibyte sequence
此时,需要对代码做一个小的调整,就可以读入中文,即以中文二进制'rb'读入txt,然后转换为'utf-8',具体代码如下:
#coding:utf-8 def readFile(): fp = open('emotion_dict//neg//neg_all_dict.txt','rb') list = [] for line in fp.readlines(): line = line.strip() line = line.decode('utf-8') list.append(line) fp.close() print(list) readFile()
相关文章推荐
- python与sqlite处理中文字符时出现的编码错误问题解决
- python中文编码错误情况
- python替换UTF-8编码文本中任意特殊字符,包括中文符号
- python替换UTF-8编码文本中任意特殊字符,包括中文符号问题:大量文本,将其中的特殊字符用空
- 解决Python print 输出文本显示 gbk 编码错误问题
- 关于java调用python2.7 脚本时遇到的中文编码错误,让人头疼的事情,浪费一天时间
- 【python】logging日志模块写入中文编码错误解决办法
- 不能使用文本编码“简体中文(Mac OS)”来打开文件“**.txt”问题的解决(转)
- python 读取并显示 UTF-8 中文文本
- [原创]python中文编码老是出错问题。
- python 处理中文时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方法
- python 中文编码 问题
- python中的中文编码问题
- 一个检查文本编码类型的Python库
- python 中文编码
- Python 中文 编码问题
- python中文字符串编码处理
- python中文字符串数组编码的问题
- Python 中文编码问题
- NopCommerce学习:MSSQL 2005 排序规则导致中文编码错误