天天看點

yaml格式和讀取

yaml檔案中格式

  1. 鍵值對
name: tome
assword: 123
           
  1. 清單
-100
	-abc
	-
		-bb
		-cc1
           
  1. 混合類型(清單裡面有字典)
a: tome
b:
	c: 10
	d: 20
info:
	- 10
	- 20
           
  1. .引号用法 雙引号 單引号
a: hello\nword
b: 'hello\nword' #特殊字元 原格式輸出
c:"hello\nword" #轉義後輸出效果
           
  1. 引用變量
#‘&’ 和‘*’用于引用
info: &ccc abc
data:
	- *ccc
	- sq
	- 200
           

讀取Yaml

#####一個Yaml檔案中多個不同資料

name: aa

password: bb

—3個‘-’表示分割線隔開

name: cc

password: 123

#########讀取單個yaml

def get_yaml(fileDir):

fo=open(fileDir,‘r’,encoding=‘utf-8’)

res=yaml.load(fo,Loader=yaml.FullLoader)

return res

######33######讀取多個yaml

def get_yaml(fileDir):

resList=[]

fo=open(fileDir,‘r’,encoding=‘utf-8’)

res=yaml.load_all(fo,Loader=yaml.FullLoader)

for item in res:

resList.append(item)

return resList