如何打印 YAML 字符串的特定部分
How do I print a specific part of a YAML string
我的 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 的 regex
模块提取 [ ]
.
import re
txt = "Present | dūc[ō] | dūc[is] | dūc[it] | dūc[imus] | dūc[itis] | dūc[unt]"
parts = txt.split(" | ")
print(parts)
# ['Present', 'dūc[ō]', 'dūc[is]', 'dūc[it]', 'dūc[imus]', 'dūc[itis]', 'dūc[unt]']
pattern = re.compile("\[(.*?)\]")
output = []
for part in parts:
match = pattern.search(part)
if match:
# group(0) is the matched part, ex. [ō]
# group(1) is the text inside the (.*?), ex. ō
output.append(match.group(1))
else:
output.append(part)
print(" | ".join(output))
# Present | ō | is | it | imus | itis | unt
代码首先将文本拆分为单独的部分,然后遍历每个部分 search
-ing for the pattern [x]
. If it finds it, it extracts the text inside the brackets from the match object 并将其存储在列表中。如果 part
与模式不匹配(例如 'Present'
),它只是按原样添加。
最后,将所有提取的字符串 join
合并在一起以重新构建没有括号的字符串。
编辑 基于 :
如果您只需要 [ ]
中的一个字符串,您可以使用相同的正则表达式模式,但对整个 txt
使用 findall
方法,这将 return list
个匹配字符串 与它们被发现的顺序相同 。
import re
txt = "Present | dūc[ō] | dūc[is] | dūc[it] | dūc[imus] | dūc[itis] | dūc[unt]"
pattern = re.compile("\[(.*?)\]")
matches = pattern.findall(txt)
print(matches)
# ['ō', 'is', 'it', 'imus', 'itis', 'unt']
那么只需要使用一些变量来 select 列表中的一个项目:
selected_idx = 1 # 0-based indexing so this means the 2nd character
print(matches[selected_idx])
# is
我的 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 的 regex
模块提取 [ ]
.
import re
txt = "Present | dūc[ō] | dūc[is] | dūc[it] | dūc[imus] | dūc[itis] | dūc[unt]"
parts = txt.split(" | ")
print(parts)
# ['Present', 'dūc[ō]', 'dūc[is]', 'dūc[it]', 'dūc[imus]', 'dūc[itis]', 'dūc[unt]']
pattern = re.compile("\[(.*?)\]")
output = []
for part in parts:
match = pattern.search(part)
if match:
# group(0) is the matched part, ex. [ō]
# group(1) is the text inside the (.*?), ex. ō
output.append(match.group(1))
else:
output.append(part)
print(" | ".join(output))
# Present | ō | is | it | imus | itis | unt
代码首先将文本拆分为单独的部分,然后遍历每个部分 search
-ing for the pattern [x]
. If it finds it, it extracts the text inside the brackets from the match object 并将其存储在列表中。如果 part
与模式不匹配(例如 'Present'
),它只是按原样添加。
最后,将所有提取的字符串 join
合并在一起以重新构建没有括号的字符串。
编辑 基于
如果您只需要 [ ]
中的一个字符串,您可以使用相同的正则表达式模式,但对整个 txt
使用 findall
方法,这将 return list
个匹配字符串 与它们被发现的顺序相同 。
import re
txt = "Present | dūc[ō] | dūc[is] | dūc[it] | dūc[imus] | dūc[itis] | dūc[unt]"
pattern = re.compile("\[(.*?)\]")
matches = pattern.findall(txt)
print(matches)
# ['ō', 'is', 'it', 'imus', 'itis', 'unt']
那么只需要使用一些变量来 select 列表中的一个项目:
selected_idx = 1 # 0-based indexing so this means the 2nd character
print(matches[selected_idx])
# is