简体   繁体   English

地址错误:(unicode error)'unicodeescape'编解码器无法解码

[英]Address error: (unicode error) 'unicodeescape' codec can't decode

Code to show image is giving a syntax error when I use the Address to the image.当我使用图像的地址时,显示图像的代码会出现语法错误。

from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

Scare = Tk()
Scare.title('?????')
Countdown = 2
CountTotal = 2
CountTotal = IntVar()

def CountdownWork():
    global Countdown
    if Countdown > 0:
        Countdown = Countdown -1
        CountTotal.set(Countdown)
        Scare.after(1000, CountdownWork)
    else:
        ImageAddress = 'C:\Users\KINSLED\Desktop\New folder\ScareTest.jpg'
        ImageItself = Image.open(ImageAddress)
        ImageNumpyFormat = np.asarray(ImageItself)
        plt.imshow(ImageNumpyFormat)
        plt.draw()
        plt.pause(5) # pause how many seconds
        plt.close()



Count = Label(Scare, font=('arial', 10, 'bold'), textvariable=CountTotal, 
bd=30, bg='SeaGreen1', justify='right').grid(row=7,columnspan=8)

CountdownWork()

Scare.mainloop()

The Syntax Error is highlighting the space just after the equals in ImageAdress.语法错误突出显示 ImageAdress 中等号后的空格。

The Error is:错误是:

(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated\\UXXXXXXXX escape (unicode 错误)“unicodeescape”编解码器无法解码位置 2-3 中的字节:截断\\UXXXXXXXX 转义

In Python strings, the backslash "\\" is a special character, also called the "escape" character.在 Python 字符串中,反斜杠"\\"是一个特殊字符,也称为“转义”字符。 It is used in representing certain whitespace characters: "\\t" is a tab, "\\n" is a newline, and "\\r" is a carriage return.它用于表示某些空白字符: "\\t"是制表符, "\\n"是换行符, "\\r"是回车符。

I believe the error is referencing your adress, specifically the special character "\\" in it.我相信错误是引用了您的地址,特别是其中的特殊字符"\\" You cannot use "\\" in your string as it will escape the string.您不能在字符串中使用"\\" ,因为它会转义字符串。 You could try using "\\\\" in your address, I think this should work.您可以尝试在地址中使用"\\\\" ,我认为这应该可行。

Please see here for futher reading on the subject: http://www.pitt.edu/~naraehan/python2/tutorial7.html有关该主题的进一步阅读,请参见此处: http : //www.pitt.edu/~naraehan/python2/tutorial7.html

The error lies in the way you've typed the file path.错误在于您输入文件路径的方式。 Windows uses backslashes \\ to separate files and directories in filenames, but any time the interpreter sees these special characters, it looks for an unicode escape sequnce, like \\n . Windows 使用反斜杠\\来分隔文件名中的文件和目录,但是任何时候解释器看到这些特殊字符时,它都会查找 unicode 转义序列,例如\\n To insert a backslash you need to insert \\\\ , one slash to trigger the escape sequence and another to indicate the backslash itlesf as the desired special character.要插入反斜杠,您需要插入\\\\ ,一个斜杠触发转义序列,另一个将反斜杠 itlesf 指示为所需的特殊字符。

Your assignment then becomes你的任务然后变成

ImageAddress = 'C\\:Users\\KINSLED\\Desktop\\New folder\\ScareTest.jpg'

which does't throw any error on my simulation.这不会在我的模拟中引发任何错误。

而不是在python中使用\\ use /这种方式你可以克服这个“(Unicode错误)'unicodeescape'编解码器无法解码位置2-3的字节:截断\\ UXXXXXXXX转义”错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (unicode错误)'unicodeescape'编解码器无法解码字节 - 字符串带'\\ u' - (unicode error) 'unicodeescape' codec can't decode bytes - string with '\u' python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误 - python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error unicode错误)“ unicodeescape”编解码器无法解码位置9-10中的字节 - unicode error) 'unicodeescape' codec can't decode bytes in position 9-10 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXXX escape , on an image - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape , on an image Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义 - Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escap SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置0-1的字节:格式错误的\\ N字符转义 - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape (unicode错误)'unicodeescape'编解码器无法解码位置16-17中的字节:截断\\ uXXXX转义 - (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \uXXXX escape Tkinter:SyntaxError:(unicode 错误)“unicodeescape”编解码器无法解码位置 2-3 中的字节:截断的 \\UXXXXXXXX 转义 - Tkinter: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape SyntaxError:(unicode 错误)“unicodeescape”编解码器无法解码 position 7-8 中的字节:截断 \UXXXXXXXX 转义 - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 7-8: truncated \UXXXXXXXX escape SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM