I tried reading a CSV file using Python

Execution environment

・ 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

I tried reading a CSV file using Python
I tried reading data from a file using Node.js.
[Python] I tried using OpenPose
I tried to touch the CSV file with Python
I tried running python etc. from a bat file
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I tried using Python (3) instead of a scientific calculator
I made a Line-bot using Python!
Python CSV file reading and writing
I tried using Thonny (Python / IDE)
[Python] I tried using YOLO v3
I tried to make a stopwatch using tkinter in python
I tried playing a ○ ✕ game using TensorFlow
I tried drawing a line using turtle
I tried using Bayesian Optimization in Python
I tried using UnityCloudBuild API from Python
I tried a functional language with Python
I tried using pipenv, so a memo
I made a configuration file with Python
I tried to convert a Python file to EXE (Recursion error supported)
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to make a todo application using bottle with python
[Python] Read a csv file with a large data size using a generator
I tried using parameterized
I tried using argparse
I made a python dictionary file for Neocomplete
I tried using mimesis
Create a GIF file using Pillow in Python
I tried using anytree
vprof --I tried using the profiler for Python
[Note] File reading ~ Python ~
I tried web scraping using python and selenium
[Python] Split a large Flask file using Blueprint
I tried playing a typing game in Python
I tried using Summpy
I tried Python> autopep8
CRLF becomes LF when reading a Python file
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
[Memo] I tried a pivot table in Python
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using Pythonect, a dataflow programming language.
I tried using PyCaret
I tried using cron
I tried using the Datetime module by Python
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
Run a Python file from html using Django
I tried adding a Python3 module in C
I tried using PyCaret
I tried using Heapq
I tried running alembic, a Python migration tool