【发布时间】:2018-12-03 22:30:46
【问题描述】:
下面是我的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,否则应返回 0
下面是我搜索描述的代码。
#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返回一个字典。不保留冒号 -
好的。如果我删除冒号,我也无法搜索字符串。它总是只返回零@cricket_007
-
string in content只检查字典的键,不检查任何值 -
那么如何检查这些值呢? @cricket_007
-
不清楚您在搜索什么。你绝对需要解析yaml,还是只需要检查文件是否包含
string?如果是后者,那么你可以return 1 if string in open(filename).read() else 0
标签: python python-2.7 yaml