下面是我的 yaml 文件
filename:
#Name of the JMX files which needs to be executed
- URLLogin.txt
- URLupload.txt
- XlsxFileUpload.txt
URLLogin:
- this is to test the script
XlsxFileUpload:
- this is to test the current script
我将把文件名存储在一个数组中。并调用一个方法来循环获取文件描述。如果文件名存在描述,则应返回 1,否则应返回零
下面是我用于搜索描述的代码。
#this method is to search a particular string in yaml
def searchStringInYaml(filename,string):
with open(filename, 'r') as stream:
content = yaml.load(stream)
if string in content:
print string
return 1
else:
return 0
stream.close()
最佳答案
yaml.load(stream)
返回一个字典,使用 content.items()
迭代并检查值
例如:
import yaml
with open(filename, 'r') as stream:
content = yaml.load(stream)
for k,v in content.items():
if "URLLogin.txt" in v:
print k, v
输出:
filename ['URLLogin.txt', 'URLupload.txt', 'XlsxFileUpload.txt']
关于python - 如何搜索 yaml 文件中的特定字符串和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51021929/