简体   繁体   中英

How to read csv file with python 3

I want to read a csv file with python3.7, but my code gives me the following error:

Traceback (most recent call last):
  File "python_to_csv.py", line 6, in <module>
    for row in csv_data:
  File "/usr/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 24: invalid start byte

Here is my code:

import csv
csv_data =  csv.reader(open('videos.export-full.csv', 'r'), delimiter=';') 
for row in csv_data:
    print (row)

How can I solve this issue and insert the records in the database? You can download a copy of the csv file from http://li2146-47.members.linode.com/videos.export-full.csv to test it yourself.

Change the following

import csv

csv_data =  csv.reader('videos.export-full.csv')

for row in csv_data:

print (row)

It should work

'\xb3' is the unicode code for ³ (SUPERSCRIPT THREE). The error is a hint that the file is not UTF-8 encoded but is probably ISO-8859-1 (or Latin1) encoded. So you should use:

csv_data =  csv.reader(open('videos.export-full.csv', 'r', encoding='Latin1'), delimiter=';')

You should control the data because Latin1 is able to convert any byte whatever the encoding, but if encoding is not ISO-8859-1, you do not get the expected characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM