python - 如何使用包含用 YAML 加载的单引号的键从集合中获取值?
问题描述
找不到关于这个问题的任何材料。
我有一个 yaml 文件,其中包含:
items :
- "jeweller's orb"
然后我加载它:
with open('./configs/items.yaml') as f:
data = yaml.safe_load(f)
self.item_set = set([x.upper() for x in data['items']])
现在如何使用包含单引号的键访问集合?我试过转义它,双引号它,没有任何效果......
print('JEWELLER\’S ORB' in self.item_set)
print("JEWELLER\’S ORB" in self.item_set)
print('JEWELLER’S ORB' in self.item_set)
print("JEWELLER’S ORB" in self.item_set)
print(r'JEWELLER’S ORB' in self.item_set)
print(r"JEWELLER’S ORB" in self.item_set)
他们都返回假。我不知道幕后有 yaml 的东西?
我还尝试复制 self.item_set 中变量的输出并直接在打印中使用它。依然没有。
编辑: 答案是
with open('./configs/items.yaml', 'rt', encoding='utf8') as f:
以及在 yaml 中使用开头的单引号版本而不是直接单引号。哇,好痛苦。
解决方案
with open('./configs/items.yaml', 'rt', encoding='utf8') as f:
以及在 yaml 中使用开头的单引号版本而不是直接单引号。哇,好痛苦。