Solve – (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape is an error which occurs when you use a normal string as a path in Python.

In this article I will try to explain exactly why this error happens and how it can be avoided, also I am going to share a nice solution that worked for me and other solutions which worked for other people.

Describing (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

In our example the error happens when we try to read a .csv file with Python.

If you want to replicate this error copy the code bellow to your editor of choice.

                                                                       #
import csv
data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
data = csv.reader(data)  
print(data)
                                                                       #

After running the code above, this is the error code we get.

                                                                       #
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
                                                                       #

This error is very common, you are not the only one, after replicating the error and trying to solve it, bellow is the solution that worked for me and other solutions which worked for other devs.

Solution 1 : use pandas.read_csv or use the r character with pandas.read_csv

The error occurs simply because we are using a string as PATH which starts a conflict that causes the error.

One line of code is enough to solve the issue, use one of the two lines of codes bellow.

                                                                       #
pandas.read_csv("C:/Users/DeePak/Desktop/myac.csv")
                                                                       #

I hope this solved your issue, if that is not the case, please include the r character as follows, it is called the raw r. It converts the string to a raw string, you can read more about it in the documentation.

                                                                       #
pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")
                                                                       #

This is another example of solving the error with a raw r.

                                                                       #
data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
                                                                       #

Solution 2 : add a double slash after the drive symbol

Another solution is to simply add a double slash after the drive symbol. Bellow are two PATH lines, the PATH before the change and the PATH after the change.

                                                                       #
"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"
                                                                       #

I hope this solved your issue, If this has been useful please consider donating to our Kofi account, there is a red button on top of this page, it is a donation, you do not have to do it, any help is appreciated.

Solution 3 : add a double backslash to the string

Here is the last solution with a detailed explanation.

The first backslash in the string is interpreted as a special character because the U after it is interpreted as the beginning of a unicode code point. The solution is to add a double backslash to the string.

                                                                       #
data = open("C:\\Users\\miche\\Documents\\school\\jaar2\\MIK\\2.6\\vektis_agb_zorgverlener")
                                                                       #

Summing-up

I hope this article has been helpful and helped you solve this error, the first solution should be enough and should work for Windows and Linux.

I hope you continue coding and learning Python, errors are part of the fun even when we hate them, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/