#the requirement to find image:

#when the value is a list or dic, you need to drive into it, and to see if images in it

def travelDict(dict_a):

if isinstance(dict_a,dict) : #使用isinstance检测数据类型

for x in range(len(dict_a)):

temp_key = dict_a.keys()[x]

temp_value = dict_a[temp_key]

if(temp_key == "image"):

print"%s : %s" %(temp_key,temp_value)

#judge the value's format of the key

if isinstance(temp_value, list):

for dic in temp_value:

travelDict(dic)

travelDict(temp_value)

def getImages(fd):

strCont = fd.read()

#convert to dict:

dicCont = yaml.load_all(strCont)

for dic in dicCont:

print(dic)

print("\n\n")

travelDict(dic)

print("\n\n")

if __name__ == "__main__":

#open the target yaml file:

#getImages(open("./deamonsetSample.yaml", "r"))

getImages(open("./test.yaml", "r"))

注意,使用load_all就是为了一次读取多个合并的yaml文件;

# 2 使用python的re包来清除字符

Python 2.7.15+ (default, Nov 27 2018, 23:36:35)

[GCC 7.3.0] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import re

>>> print re.sub('[\r\t\n\'\" ]', "", "hello \n \thello kiss")

hellohellokiss

#1 使用python递归读数据和字典,主要用于处理yaml文件如下代码,递归读取字典,当其中的值为list的时候,接着递归:```import yamlimport osimport re#the requirement to find image:#when the value is a list or dic, you need to drive in... AWSTemplateFormatVersion : ' 2010-09-09 ' Resources : - $ref : ./relative/path/to/file. yaml RolePolicies : $ref : ./resources/role-policies. yaml Type : ' AWS::IAM::Policy ' Properties : PolicyName : custom-role Roles : - custom-role PolicyDocument : Version : ' 2012-10-17 ' Statement :
在自动化框架搭建中,一些配置 文件 (例如:数据库账号、url,日志级别等...)会经常的用到,所以我们需要一个存放这些参数的 文件 ,并从中 读取 和写入。当然,存放参数除了用 yaml ,像ini 文件 、json 文件 、excel 文件 也是可以的,看个人选择。本文就介绍 yaml 文件 的操作方法。 1、准备一个 yaml 文件 ,并存放一些测试数据 2、准备好数据后,就新建一个py 文件 ,用于编写操作 yaml 文件 的脚本。在开始之前我们需要导入 yaml 库 import yaml 3、 读取 yaml 文件 中所有数据,首先需要使用ope
# 创建一个 YAML 文件 data = {'name': 'John', 'age': 30, 'city': 'New York'} with open('data. yaml ', 'w') as file: documents = yaml .dump(data, file) # 读取 YAML 文件 中的 变量 with open('data. yaml ') as file: documents = yaml .full_load(file) print(documents['name']) # 输出John # 修改 YAML 文件 中的 变量 with open('data. yaml ') as file: documents = yaml .full_load(file) documents['age'] = 35 with open('data. yaml ', 'w') as file: yaml .dump(documents, file) 这段代码创建了一个 YAML 文件 读取 了其中的 变量 并输出了其中的一个 变量 ,然后修改了其中的一个 变量 并将其保存回 文件 中。 [VRFC 10-529] concurrent assignment to a non-net an is not permitted ["C:/Users/chenxy/Desktop/digit 19008