Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]

Introduction

I have summarized the reasons why the following error appears when reading a csv file with python.

 import pandas as pd
pd.read_csv("file/to/path")

I hope it will be helpful for those who have the following code when reading_csv with pandas.

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

1. Solution

To conclude first, please use the code below.

pd.read_csv("file/to/path", encoding="shift-jis")

encoding="shift-jis"

If you just put it on, it should be OK! If you still get the error, read 2 and later and consider the reason.

2. Why you get an error

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

In the first place, what makes this error angry is rough. It says that data cannot be read if the character code "utf-8" is used.

For example, I can't read English, so change it to Japanese and then read the data! It's like that.

So let's convert it to another character code. Converting that character code is called encoding.

3. Then why is "shift-jis"?

In order to explain it, let me briefly explain the typical character code.

UTF-8 It is the most popular character code in the world and is one of the encoding methods for Unicode. It is established for the purpose of using the characters defined in ASCII as they are in Unicode.

Aside from the difficult things here, it's okay to recognize that it is the most used.

JIS code

It is a standard character code on the Internet, especially for e-mail.

EUC Abbreviation for Extended Unix Code, used by Japanese UNIX.

Shift_JIS A code developed by Microsoft, which is ASCII code characters with Japanese characters added. It is also used on Windows and Mac, and is widely used for files on PCs.

** In other words, the csv file containing Japanese cannot be read in UTF-8, so change it to Shift_JIS **

For those who could not read the data encoded in Shift_JIS Think about which of the other character codes the file fits into and try it.


By the time you write a memo

Recommended Posts

Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
[Python] Write to csv file with Python
Output to csv file with Python
[Python: UnicodeDecodeError] One of the error solutions when reading CSV
How to read a CSV file with Python 2/3
I tried to touch the CSV file with Python
Error due to conflict between python when using gurobi
How to convert JSON file to CSV file with Python Pandas
[Introduction for beginners] Reading and writing Python CSV files
Error when playing with python
Write to csv with Python
~ Tips for beginners to Python ③ ~
Download csv file with python
Permission error when reading Django upload files
[Python] Reading CSV files
Reading and writing csv files
[Python: UnicodeDecodeError] One of the error solutions when reading CSV
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
[Python] What to check when you get a Unicode Decode Error in Django
Python CSV file reading and writing
Reading and writing CSV with Python
[Pandas] I tried to analyze sales data with Python [For beginners]
Atom: Note for Indentation Error when copying Python script to shell
[Python] How to convert db file to csv
INSERT into MySQL with Python [For beginners]
Memo to ask for KPI with python
Read CSV file with python (Download & parse CSV file)
[Python] Convert csv file delimiters to tab delimiters
[Python] Read images with OpenCV (for beginners)
[Part1] Scraping with Python → Organize to csv!
Python> Output numbers from 1 to 100, 501 to 600> For csv
WebApi creation with Python (CRUD creation) For beginners
[For beginners] Try web scraping with Python
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
How to deal with OAuth2 error when using Google APIs from Python
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
Read CSV file with Python and convert it to DataFrame as it is
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Error when installing a module with Python pip
The fastest way for beginners to master Python
Scraping tabelog with python and outputting to CSV
Causal reasoning and causal search with Python (for beginners)
CRLF becomes LF when reading a Python file
[Python] How to read excel file with pandas
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Convert svg file to png / ico with Python
Writing logs to CSV file (Python, C language)
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
Character code for reading and writing csv files with python ~ windows environment ver ~
Output timing is incorrect when standard (error) output is converted to a file in Python
Try to calculate RPN in Python (for beginners)
I tried reading a CSV file using Python
Memo # 2 for Python beginners to read "Detailed Python Grammar"
What to do if you get an error when installing python with pyenv
Write CSV data to AWS-S3 with AWS-Lambda + Python
Introduction to Programming (Python) TA Tendency for beginners
Error when trying to install psycopg2 in Python
Memo # 6 for Python beginners to read "Detailed Python Grammar"
I want to write to a file with Python
How to make Python faster for beginners [numpy]
~ Tips for Python beginners from Pythonista with love ② ~
Materials to read when getting started with Python
Memo # 5 for Python beginners to read "Detailed Python Grammar"
[Introduction for beginners] Working with MySQL in Python