这是我的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()
答案 0 :(得分:1)
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']
答案 1 :(得分:0)
如果只需要检查yaml文件中的指定字符串,请不要解析yaml文件。只需阅读文件并检查内容
def searchStringInYaml(filename,string):
with open(filename) as f:
contents = f.read()
return string in contents