Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从包含加载了YAML的单引号的键的集合中获取值?_Python_Yaml - Fatal编程技术网

Python 如何从包含加载了YAML的单引号的键的集合中获取值?

Python 如何从包含加载了YAML的单引号的键的集合中获取值?,python,yaml,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(

找不到有关此问题的任何资料

我有一个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_集中变量的输出,并在打印中直接使用它。还是没什么

编辑: 答案是

with open('./configs/items.yaml', 'rt', encoding='utf8') as f:
以及在yaml中使用开头单引号版本,而不是直接单引号。哇,好痛啊

with open('./configs/items.yaml', 'rt', encoding='utf8') as f:

以及在yaml中使用开头单引号版本,而不是直接单引号。哇,真痛苦。

试试
“珠宝商之珠”
这能回答你的问题吗?还请注意,您在代码中使用的单引号与yaml文件中的单引号不同。
项目集中的“珠宝商之珠”
一些(富文本)编辑器使用unicode引号,而不是普通的ascii引号。在比较python中的代码时,它们并不相等(不同的unicode代码点)。看看你问题中的第一次尝试。这些字符的渲染方式不同。
with open('./configs/items.yaml', 'rt', encoding='utf8') as f: