当前位置:首页 > 日记本 > 正文内容

python 读取txt UnicodeDecodeError: 'utf-8' codec can't decode byte

zhangchap2年前 (2022-03-30)日记本190

因为写入的时候指定的是utf-8,语法也没错,百度后才得知,加上

errors='ignore'
open('baidu_url.txt', 'r', encoding='utf-8',errors='ignore')

如果设置为ignore,则会忽略非法字符;

如果设置为replace,则会用?取代非法字符;

如果设置为xmlcharrefreplace,则使用XML的字符引用。


分享给朋友:

相关文章

Nginx+PHP,PHP如何优化配置?

具体修改FPM配置文件参数: 若你的php日志出现: WARNING: [pool www] seems busy (you may need to increase pm.sta...

python 函数 开启多线程示例

from threading import Thread def readfile(queue:Queue):    &nbs...

python xpath语法总结

python xpath语法总结:常用的://1.从任意节点开始/2.从根节点开始//div/p3.div下的p标签//div[@class="hrzz_bottom"]/ul/l...

python 发布文章 随机分类(choice)

from random import choice catid = choice([5,6]) #choice 函数从列表中随机提取...

python 获取当前时间及随机时间戳

import time from random import randint time.strftime('%Y-%m-%d %H:%M:...

python url.parse模块编码解码

from urllib.parse import quote,unquote,urlencode # 对汉字进行编码使用 quote ...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。