报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated
程序员文章站
2022-06-15 13:42:40
...
测试opencv是否安装成功时报错:
File "<ipython-input-3-85edcd6573e7>", line 3
img = cv.imread("C:\Users\Administrator\Desktop\daima\ch1\1.png")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
解决办法:
路径书写方式错误;
错误的:
cv.imread("C:\Users\Administrator\Desktop\daima\ch1\1.png")
修改后:
img = cv.imread("C:\\Users\\Administrator\\Desktop\\daima\\ch1\\1.png")
原因:
windows在读取文件可以用\,但是在字符串中\是被当作转义字符来使用。
python在描述路径时的方式:
方式 | 写法 |
---|---|
转义的方式 | ‘d:\a.txt’ |
显式声明字符串不用转义 | ‘d:r\a.txt’ |
使用Linux的路径/ | ‘d:/a.txt’ |
下一篇: MySQL分区表使用方法
推荐阅读
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX
-
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX