我有一個包含以下內容的 yaml 檔案:
testcases:
- testcase_1:
username:
password:
- testcase_2:
username:
password:
我如何遍歷過度的內容。我想首先使用 testcases_1 中存在的變數運行測驗用例,然后使用 testcase_2 中的變數運行測驗用例。? 我該怎么做呢 ?///
uj5u.com熱心網友回復:
您可以使用yaml.safe_load函式來加載內容,然后您可以對其進行迭代:
import yaml
content = None
with open("D:/data.yml") as f:
try:
content = yaml.safe_load(f)
except yaml.YAMLError as e:
print(e)
print(content)
for item in content['testcases']:
print(item)
上面的代碼會輸出:
{'testcases': [{'testcase_1': {'username': None, 'password': None}}, {'testcase_2': {'username': None, 'password': None}}]}
{'testcase_1': {'username': None, 'password': None}}
{'testcase_2': {'username': None, 'password': None}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/404569.html
標籤:
上一篇:基于一鍵匹配值合并字典串列
下一篇:如何根據字典的值字符快速過濾字典
