・ Mac OS X
I wanted to browse CSV files from Python, so I actually tried it.
First, I tried to use the code of the CSV module usage example in the official Python document as it is.
read.py
import csv
with open('hoge.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
Let's run it in the terminal immediately.
$ python3 read.py
Then, I got the following error.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8e in position 1: invalid start byte
There seems to be a problem with character encoding. So, I tried to specify the encoding to cp932 as follows.
read.py
import csv
with open('hoge.csv', newline='', encoding='cp932') as f:
reader = csv.reader(f)
for row in reader:
print(row)
The error was resolved and I was able to successfully open the csv file from the Python script.
Recommended Posts