成功解决Python中导出图片出现错误SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position

成功解决Python中导出图片出现错误SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position


解决问题

今天对Python编程写个Hierarchical clustering层次聚类算法,想把多张图片层次聚类,输出一张图片时,发现了一个讨厌的bug

drawdendrogram(tree, imlist, jpeg='C:\Users\99386\Desktop\result.jpg')  #

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

解决方法

drawdendrogram(tree, imlist, jpeg='C:\Users\99386\Desktop\result.jpg')  #
将上边代码更为以下的方式就好啦,已经测试过,下边两种办法皆可以,看心情选择啦
理解:原来Python读取和导出的格式还是有区别的!

方法1:双斜杠代替单斜杠
drawdendrogram(tree, imlist, jpeg='C:\\Users\\99386\\Desktop\\result.jpg')

方法2:前边加个字母r

drawdendrogram(tree, imlist, jpeg=r'C:\Users\99386\Desktop\result.jpg') 

大功告成!

(0)

相关推荐