In this post, you can find several solutions for:

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

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

  • there are 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

Step #1: How to solve SyntaxError: (unicode error) 'unicodeescape' - Double slashes for escape characters

Let's start with one of the most frequent examples - windows paths. In this case there is a bad character sequence in the string:

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)

Step #2: Use raw strings to prevent SyntaxError: (unicode error) 'unicodeescape'

If the first option is not good enough or working then raw strings are the next option. Simply by adding r (for raw string literals) to resolve the error. This is an example of raw strings:

import json

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

If you like to find more information about Python strings, literals

2.4.1 String literals

In the same link we can find:

When an r' or R' prefix is present, backslashes are still used to quote the following character, but all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase `n'.

Step #3: Slashes for file paths -SyntaxError: (unicode error) 'unicodeescape'

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 use only the second way in order to avoid such nasty traps. Another reason for using slashes is your code to be uniform and homogeneous.

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

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

SyntaxError_unicode_error_unicodeescape

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 produces the error and click on it. Once the reason is identified then you can test what could solve the problem.