Python读csv报'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte问题

'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte

Python读取csv文件时可能会遇到乱码或者报错:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte

只要把encoding= utf-8改成gbk即可
image
image

知其所以然,异常其实是读取的csv文件的编码导致的,通常有中文的csv文件中,excel会将其编译为ANSI的格式。我们可以通过用记事本打开查看到编码格式。同样的在记事本中另存为,可以修改编码格式为UTF-8,这样就能治标治本啦!
image
image

posted @ 2022-01-23 15:20  HEREISDAVID  阅读(3338)  评论(0编辑  收藏  举报