欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-15 13:50:23
...

D:\code\draw_path\venv\Scripts\python.exe D:/code/draw_path/dispaly_pic/draw_pic_2.py
  File "D:/code/draw_path/dispaly_pic/draw_pic_2.py", line 9
    plt.savefig("C:\Users\octopus\Desktop\html")
               ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape


python出现报错问题:应该是转义字符有错,在前面加上r代表原来意思就没有出现这种错误了。

# encoding='utf-8'
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,10,0.2)
y=np.sin(x)

fig,ax=plt.subplots()
ax.plot(x,y)
plt.savefig("C:\Users\octopus\Desktop\html")
plt.show()

应该改为:

# encoding='utf-8'
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,10,0.2)
y=np.sin(x)

fig,ax=plt.subplots()
ax.plot(x,y)
plt.savefig(r"C:\Users\octopus\Desktop\html")
plt.show()

 

相关标签: python python基础