在我正在进行的项目中,我必须加载几十张图像。但是,如果我尝试加载其中任何一个,如下所示:
twoc = pygame.image.load("C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png")
我收到此消息:
"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\python.exe" "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py"
File "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py", line 16
twoc = pygame.image.load("C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png") # Lines 15-66 bring all the cards into the program
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
我不知道我做错了什么。有没有人可以帮助我?
更新:所以我走了,用/替换了文件地址中的每个\。再次,它完美地工作,直到:
"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\python.exe" "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py"
Traceback (most recent call last):
File "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py", line 28, in <module>
fivc = pygame.image.load("C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/5_of_clubs")
pygame.error: Couldn't open C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/5_of_clubs
Process finished with exit code 1
答案 0 :(得分:2)
公开错误的最小示例:
s = "\U"
在Python中,反斜杠用作转义序列和\Uxxxx
pattern is used to declare unicode characters while keeping source code ascii-only。通过在字符串中使用\Users
,\U
之后的字符串是无效的十六进制数,因此会引发异常。
最快速的修复 - mark string as raw:
s = r"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png
答案 1 :(得分:1)
基本上你需要在字符串中添加一个转义斜杠和另一个斜杠。所以你最终会得到两个斜线,如下:
twoc = pygame.image.load("C:\\Users\\Z & Z Azam\\AppData\\Local\\Programs\\Python\\Python35\\Scripts\\Cards\\2_of_clubs.png")
或者你要做的另一种方法是将反斜杠改为正斜杠:
twoc = pygame.image.load("C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/2_of_clubs.png")