Python Pillow having error with dictionaries nested in list
Question:
I am creating a list of nested dictionaries to have the file addresses for images I will be using for my program. However, in the BU0 dictionary it seems to cause an error that reads:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 20-21: truncated UXXXXXXXX escape
Here is the nested list:
Bases = [
{
"BC0": ["Owl_project_picturesCommonGreat Grey Owl_greatgrey_base.PNG", "Owl_project_picturesCommonGreat Grey Owl_greatgrey_bonus1.PNG",
"Owl_project_picturesCommonGreat Grey Owl_greatgrey_bonus2.PNG", "Owl_project_picturesCommonGreat Grey Owl_greatgrey_accent.PNG",
"Owl_project_picturesCommonGreat Grey Owl_greatgrey_shadows.PNG", "Owl_project_picturesCommonGreat Grey Owl_greatgrey_eyesbeakfeet.PNG",
"Owl_project_picturesCommonGreat Grey Owl_greatgrey_lines.PNG"],
"BC1": ["Owl_project_picturesCommonBarn Owl_barn_base.PNG", "Owl_project_picturesCommonBarn Owl_barn_bonus1.PNG",
"Owl_project_picturesCommonBarn Owl_barn_bonus2.PNG", "Owl_project_picturesCommonBarn Owl_barn_bonus2.PNG",
"Owl_project_picturesCommonBarn Owl_barn_accent.PNG", "Owl_project_picturesCommonBarn Owl_barn_shadows.PNG",
"Owl_project_picturesCommonBarn Owl_barn_eyesbeakfeet.PNG", "Owl_project_picturesCommonBarn Owl_barn_lines.PNG"],
"BC2": ["Owl_project_picturesCommonBurrowing Owl_burrowing_base.PNG","Owl_project_picturesCommonBurrowing Owl_burrowing_bonus1.PNG",
"Owl_project_picturesCommonBurrowing Owl_burrowing_bonus2.PNG", "Owl_project_picturesCommonBurrowing Owl_burrowing_accent.PNG",
"Owl_project_picturesCommonBurrowing Owl_burrowing_shadows.PNG", "Owl_project_picturesCommonBurrowing Owl_burrowing_eyesbeakfeet.PNG",
"Owl_project_picturesCommonBurrowing Owl_burrowing_lines.PNG"]
},
{
# The section that causes the error
"BU0": ["Owl_project_picturesUncommonCrested Owl_crested_base.PNG", "Owl_project_picturesUncommonCrested Owl_crested_bonus1.PNG"]
}
]
Answers:
Try to escape the backslashes:
...
{
"BU0": [
"Owl_project_pictures\Uncommon\Crested Owl\_crested_base.PNG",
"Owl_project_pictures\Uncommon\Crested Owl\_crested_bonus1.PNG",
]
}
...
Or put them as raw strings r"..."
:
...
"BU0": [
r"Owl_project_picturesUncommonCrested Owl_crested_base.PNG",
r"Owl_project_picturesUncommonCrested Owl_crested_bonus1.PNG",
]
}
...
I am creating a list of nested dictionaries to have the file addresses for images I will be using for my program. However, in the BU0 dictionary it seems to cause an error that reads:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 20-21: truncated UXXXXXXXX escape
Here is the nested list:
Bases = [
{
"BC0": ["Owl_project_picturesCommonGreat Grey Owl_greatgrey_base.PNG", "Owl_project_picturesCommonGreat Grey Owl_greatgrey_bonus1.PNG",
"Owl_project_picturesCommonGreat Grey Owl_greatgrey_bonus2.PNG", "Owl_project_picturesCommonGreat Grey Owl_greatgrey_accent.PNG",
"Owl_project_picturesCommonGreat Grey Owl_greatgrey_shadows.PNG", "Owl_project_picturesCommonGreat Grey Owl_greatgrey_eyesbeakfeet.PNG",
"Owl_project_picturesCommonGreat Grey Owl_greatgrey_lines.PNG"],
"BC1": ["Owl_project_picturesCommonBarn Owl_barn_base.PNG", "Owl_project_picturesCommonBarn Owl_barn_bonus1.PNG",
"Owl_project_picturesCommonBarn Owl_barn_bonus2.PNG", "Owl_project_picturesCommonBarn Owl_barn_bonus2.PNG",
"Owl_project_picturesCommonBarn Owl_barn_accent.PNG", "Owl_project_picturesCommonBarn Owl_barn_shadows.PNG",
"Owl_project_picturesCommonBarn Owl_barn_eyesbeakfeet.PNG", "Owl_project_picturesCommonBarn Owl_barn_lines.PNG"],
"BC2": ["Owl_project_picturesCommonBurrowing Owl_burrowing_base.PNG","Owl_project_picturesCommonBurrowing Owl_burrowing_bonus1.PNG",
"Owl_project_picturesCommonBurrowing Owl_burrowing_bonus2.PNG", "Owl_project_picturesCommonBurrowing Owl_burrowing_accent.PNG",
"Owl_project_picturesCommonBurrowing Owl_burrowing_shadows.PNG", "Owl_project_picturesCommonBurrowing Owl_burrowing_eyesbeakfeet.PNG",
"Owl_project_picturesCommonBurrowing Owl_burrowing_lines.PNG"]
},
{
# The section that causes the error
"BU0": ["Owl_project_picturesUncommonCrested Owl_crested_base.PNG", "Owl_project_picturesUncommonCrested Owl_crested_bonus1.PNG"]
}
]
Try to escape the backslashes:
...
{
"BU0": [
"Owl_project_pictures\Uncommon\Crested Owl\_crested_base.PNG",
"Owl_project_pictures\Uncommon\Crested Owl\_crested_bonus1.PNG",
]
}
...
Or put them as raw strings r"..."
:
...
"BU0": [
r"Owl_project_picturesUncommonCrested Owl_crested_base.PNG",
r"Owl_project_picturesUncommonCrested Owl_crested_bonus1.PNG",
]
}
...