我的YAML数据库:
left:
- title: Active Indicative
fill: "#cb202c"
groups:
- "Present | dūc[ō] | dūc[is] | dūc[it] | dūc[imus] | dūc[itis] | dūc[unt]"
我的Python代码:
import io
import yaml
with open("C:/Users/colin/Desktop/LBot/latin3_2.yaml", 'r', encoding="utf8") as f:
doc = yaml.safe_load(f)
txt = doc["left"][1]["groups"][1]
print(txt)
当前我的输出是Present | dūc[ō] | dūc[is] | dūc[it] | dūc[imus] | dūc[itis] | dūc[unt]
,但我希望输出是ō
、is
、it
或imus
。这在PyYaml中可能吗?如果可能的话,我将如何实现它?提前谢谢。你知道吗
我没有PyYaml解决方案,但是如果您已经拥有YAML文件中的字符串,那么可以使用Python的^{} 模块来提取
[ ]
中的文本。你知道吗代码首先将文本分割成单独的部分,然后循环遍历每个部分以获得模式
[x]
。如果找到它,它将从match object中提取括号内的文本并将其存储在列表中。如果part
与模式不匹配(例如'Present'
),它只是按原样添加它。你知道吗最后,将所有提取的字符串^{} -ed在一起,以重新构建不带括号的字符串。你知道吗
编辑基于comment:
如果只需要} 方法,该方法将返回匹配字符串的
[ ]
中的一个字符串,可以使用相同的正则表达式模式,但在整个txt
上使用^{list
,其顺序与找到它们的顺序相同。你知道吗然后只需使用一些变量从列表中选择一个项目:
相关问题 更多 >
编程相关推荐