2016-08-26 3 views
1

私が取り組んでいるプロジェクトでは、数十枚の画像を読み込む必要があります。私はこのように、それらのいずれかをロードしようとする場合は:構文unicode error pygame

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 

答えて

2

最小限の例エラー:Pythonのバックスラッシュで

s = "\U" 

は、エスケープシーケンスと\Uxxxx pattern is used to declare unicode characters while keeping source code ascii-onlyとして使用されています。文字列に\Usersを使用すると、\Uの後の文字列は無効な16進数であるため、例外が発生します。

最速修正 - mark string as raw

s = r"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png 
1

基本的に、文字列にエスケープスラッシュを追加する必要があります。だから、あなたがそうのような2つのスラッシュ、となってしまいます。

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")