Python SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3:

today Python digital image processing installed anaconda, using its default editor spyder. But an error occurred while running a simple program like the one shown below. The error is:

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

where the simple test code is:

 

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 21:31:25 2017

@author: harchi
"""
from skimage import io
img=io.imread('C:\Users\harchi\Desktop\图像处理\skeleton.bmp')
io.imshow(img)

the cause of the error is: imread(‘C:\Users\harchi\Desktop\ skeleton. BMP ‘) the “\” in the line represents an escape in Python.

the solution, of course, is not to let “\” stand for escape. So you can:

1, prefix the string with r or r, i.e. imread(r’C:\Users\harchi\Desktop\ skeleton. BMP ‘) where r or r denotes an unescaped string in python.

2, before “\” with “\” to achieve escape. Namely: imread (‘ C: \ \ Users \ \ harchi \ \ Desktop \ \ \ \ skeleton image processing BMP ‘) </ span> </ p>

</ span> 3, “\” change into “/”, namely: the imread (‘ C:/Users \ harchi/Desktop/image processing/skeleton BMP ‘) </ span> </ p>

</ span> finally: add python string prefix knowledge:

1, prefix the string with r or r to indicate that the string is an unescaped original string.

2, prefix a string with u or u to indicate that the string is unicode.

Read More: