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

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

A very common error in Python 3 is:

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

While this error can appear in differen situations the reason for the error is one and the same:

  • there is special characters( escape sequence - characters starting with backslash - '' ).
  • From the error above you can recognize that the culprit is '\U' - which is considered as unicode character.
    • another possible errors for SyntaxError: (unicode error) 'unicodeescape' will be raised for '\x', '\u'
      • codec can't decode bytes in position 2-3: truncated \xXX escape
      • codec can't decode bytes in position 2-3: truncated \uXXXX escape

Solutions for (unicode error) 'unicodeescape'

Double slashes for escape characters

Which means that you have bad character escape in string. Next example shows how you can get the error:

import json

json_data=open("C:\Users\test.txt").read()
json_obj = json.loads(json_data)

The problem is that \U is considered as a special escape sequence for Python string. In order to resolved you need to add second escape character like:

import json

json_data=open("C:\\Users\\test.txt").read()
json_obj = json.loads(json_data)

Use raw strings

Another way to resolved the error is simply by adding r for raw string literals:

import json

json_data=open(r"C:\Users\test.txt").read()
json_obj = json.loads(json_data)

Some best practices shows that using os module is best:

os.path.join(mydir, myfile)

The error is produced with:

  • PyCharm 2019.2
  • OS and version: Linux Mint 19, Windows 10 Home
  • Python version: 3.7

Use slashes for file paths

Another possible solution is to replace the backslash with slash for paths of files and folders. For example:

"C:\Users\test.txt"

will be changed to:

"C:/Users/test.txt"

Since python can recognize both I prefer to uses only the second way in order to avoid such nasty traps. Another reason for using slashes is your code to be uniform and homogeneous.

PyCharm: SyntaxError: (unicode error) 'unicodeescape' codec can't decode ..

The picture below demonstrates how the error will looks like in PyCharm. In order to understand what happens you will need to investigate the error log.

The error log will have information for the program flow as:

/home/vanx/Software/Tensorflow/environments/venv36/bin/python3 /home/vanx/PycharmProjects/python/test/Other/temp.py
  File "/home/vanx/PycharmProjects/python/test/Other/temp.py", line 3
    json_data=open("C:\Users\test.txt").read()
                  ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

You can see the latest call which produce the error and click on it. Once the reason is identified then you can test what could solve the problem.

SyntaxError_unicode_error_unicodeescape

Share Tweet Send
0 Comments
Loading...