admin管理员组文章数量:1611174
困扰了很久的问题今天终于解决了!!!
异步导入csv文件提示UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte
原代码为:
resp = requests.get(private_url).content
with open(file_dir_name, "w") as fd:
fd.write(resp.decode(encoding="utf-8"))
解码的字符格式我换成了gbk,Latin。。。各种都试过,只要有中文,就会报错。
年少的我不懂事,不知道翻译一看,看到这个报错想当然认为是解码的字符集不对,直到今天,我终于正视了bytes,我仿佛找到了解决门道。
于是我打印了resp的type类型,发现果然是<class 'bytes'>
, 不就是二进制字节类型吗
然后我再次看了代码,resp取的是requests的content,点进去看content的源码解释:
def content(self):
"""Content of the response, in bytes."""
响应内容就是bytes类型。
更改后的代码为:
resp = requests.get(private_url).content
with open(file_dir_name, "w") as fd:
fd.write(resp.decode(encoding="unicode_escape"))
unicode_escape就是对编码后存储的文本,读取时进行反向转换,就能直接得到原始文本数据。
最后完美解决。
感谢https://wwwblogs/my_captain/p/9092644.html
ps:想起之前的产品问我为什么不能加入中文,我和他好生辩论了一番,现在想起来还是我太菜了,遇到问题没找到解决办法就翻篇。其实这次我差点又想不让文件中有中文,但是这个产品要求一定有中文,于是我便深入了解了一下。。。没有解决不了的难题,只有一颗懒惰的怕麻烦的心~
本文标签: CodecDecodeUnicodeDecodeErrorUTFbyte
版权声明:本文标题:解决UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xc8 in position 0: invalid continuation byte 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728605679a1165419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论