How to resolve a UnicodeDecodeError for a CSV file in Python?
Last Updated :
24 Apr, 2025
Several errors can arise when an attempt to decode a byte string from a certain coding scheme is made. The reason is the inability of some encoding schemes to represent all code points. One of the most common errors during these conversions is UnicodeDecode Error which occurs when decoding a byte string by an incorrect coding scheme. This article will teach you how to resolve a UnicodeDecodeError for a CSV file in Python.
Why does the UnicodeDecodeError error arise?
The error occurs when an attempt to represent code points outside the range of the coding is made. To solve the issue, the byte string should be decoded using the same coding scheme in which it was encoded. i.e., The encoding scheme should be the same when the string is encoded and decoded.Â
For demonstration, the same error would be reproduced and then fixed. In the below code, firstly the character a (byte string) is decoded using ASCII encoding successfully. Then an attempt to decode the byte string a\xf1 is made, which led to an error. This is because the ASCII encoding standard only allows representation of the characters within the range 0 to 127. Any attempt to address a character outside this range would lead to the ordinal not-in-range error.
Python3
t = b"a".decode("ascii")
# Produces error
t1 = b"a\xf1".decode("ascii")
Output:
Traceback (most recent call last):
File "C:/Users/Sauleyayan/PycharmProjects/untitled1/venv/mad philes.py", line 5, in <module>
t1 = b"a\xf1".decode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf1 in position 1: ordinal not in range(128)
To rectify the error, an encoding scheme would be used that would be sufficient to represent the \xf1 code point. In this case, the unicode_escape coding scheme would be used:
Python3
t1 = b"a\xf1".decode("unicode_escape")
print(t1)
Output:
añ
How to Resolve a UnicodeDecodeError for a CSV fileÂ
It is common to encounter the error mentioned above when processing a CSV file. This is because the CSV file may have a different encoding than the one used by the Python program. To fix such an error, the encoding used in the CSV file would be specified while opening the file. If the encoding standard of the CSV file is known, the Python interpreter could be instructed to use a specific encoding standard while that CSV file is being opened. This method is only usable if the encoding of the CSV is known.
To demonstrate the occurrence of the error, the following CSV file will be used:
The encoding of the CSV file is UTF-16Generating UnicodeDecodeError for a CSV fileÂ
The following code attempts to open the CSV file for processing. The above code, upon execution, led to the following error:
Python3
import pandas as pd
path = "test.csv"
# The following statement reads the csv file at the given path
# While decoding the contents of the file in utf-8 decoding standard
file = pd.read_csv(path)
print(file.head())
Output:
 Understanding the Problem
The error occurred as the read_csv method could not decode the contents of the CSV file by using the default encoding, UTF-8. This is because the encoding of the file is UTF-16. Hence the encoding of the CSV file needs to be mentioned while opening the CSV file to fix the error and allow the processing of the CSV file.
Solution
Firstly, the pandas' library is imported, and the path to the CSV file is specified. Then the program calls the read_csv function to read the contents of the CSV file specified by the path and also passes the encoding through which the CSV file must be decoded (UTF-16 in this case). Since the decoding scheme mentioned in the argument is the one with which the CSV file was originally encoded, the file gets decoded successfully.Â
Python3
import pandas as pd
path = "test.csv"
# The following statement reads the csv file at the given path
# While decoding the contents of the file in utf-8 decoding standard
file = pd.read_csv(path, encoding="utf-16")
# Displaying the contents
print(file.head())
Output:
 Alternate Method to Solve UnicodeDecodeError
Another way of resolving the issue is by changing the encoding of the CSV file itself. For that, firstly, open the CSV file as a text file (using notepad or Wordpad):
Â
Now go to file and select Save as:
Â
A prompt would appear, and from there, select the encoding option and change it to UTF-8 (the default for Python and pandas), and select Save.
Â
Â
Now the following code would run without errors
The code ran without errors. This is because the default encoding of the CSV file was changed to UTF-8 before opening it with pandas. Since the default encoding used by pandas is UTF-8, the CSV file opened without error.Â
Python3
import pandas as pd
path = "test.csv"
# The following statement reads the csv file at the given path
# While decoding the contents of the file in utf-8 decoding standard
file = pd.read_csv(path)
print(file.head())
Output:
Â
Similar Reads
How to add a header to a CSV file in Python?
When you work with CSV files in Python, you might want to add a title row to make the data easier to read. A CSV file is like a table where each row is a piece of information, and each part of that information is separated by commas. By adding a title at the top, like "Name," "Age," and "Location,"
4 min read
How to Read from a File in Python
Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently.Example File: geeks.txtHello World Hello GeeksforGe
5 min read
Reading Rows from a CSV File in Python
CSV stands for Comma Separated Values. This file format is a delimited text file that uses a comma as a delimiter to separate the text present in it. Or in other words, CSV files are used to store data in tabular form. As per the name suggested, this file contains the data which is separated by a co
5 min read
How to save a Python Dictionary to a CSV File?
CSV (Comma-Separated Values) files are a popular format for storing tabular data in a simple, text-based format. They are widely used for data exchange between applications such as Microsoft Excel, Google Sheets and databases. In this article, we will explore different ways to save a Python dictiona
4 min read
How To Print Unicode Character In Python?
Unicode characters play a crucial role in handling diverse text and symbols in Python programming. This article will guide you through the process of printing Unicode characters in Python, showcasing five simple and effective methods to enhance your ability to work with a wide range of characters Pr
2 min read
How To Convert Unicode To Integers In Python
Unicode is a standardized character encoding that assigns a unique number to each character in most of the world's writing systems. In Python, working with Unicode is common, and you may encounter situations where you need to convert Unicode characters to integers. This article will explore five dif
2 min read
How to read all CSV files in a folder in Pandas?
Our task is to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folder using glob() method and then reading the file by using pandas.read_csv() method and then displaying the content.ApproachImport Required Librarie
2 min read
How to plot Bar Graph in Python using CSV file?
CSV stands for "comma separated values", that means the values are distinguished by putting commas and newline characters. A CSV file provides a table like format that can be read by almost every spreadsheet reader like Microsoft Excel and Google Spreadsheet. A Bar Graph uses labels and values where
2 min read
How To Read .Data Files In Python?
Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary w
4 min read
How To Fix Valueerror Exceptions In Python
Python comes with built-in exceptions that are raised when common errors occur. These predefined exceptions provide an advantage because you can use the try-except block in Python to handle them beforehand. For instance, you can utilize the try-except block to manage the ValueError exception in Pyth
4 min read