Python 3 CSV file giving UnicodeDecodeError: 'utf-8' codec can't decode byte error when I print

前端 未结 7 570
梦如初夏
梦如初夏 2020-12-03 01:18

I have the following code in Python 3, which is meant to print out each line in a csv file.

import csv
with open(\'my_file.csv\', \'r\', newline=\'\') as csv         


        
相关标签:
7条回答
  • 2020-12-03 01:41

    For others who hit the same error shown in the subject, watch out for the file encoding of your csv file. Its possible it is not utf-8. I just noticed that LibreOffice created a utf-16 encoded file for me today without prompting me although I could not reproduce this.

    If you try to open a utf-16 encoded document using open(... encoding='utf-8'), you will get the error:

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

    To fix either specify 'utf-16' encoding or change the encoding of the csv.

    0 讨论(0)
提交回复
热议问题