python讀取 YAML檔案

2021-10-11 05:29:49 字數 1495 閱讀 4599

yaml在我的理解看來,它是一種資料的格式,他的格式跟json很相近,但是yaml支援注釋

1.物件名 :健:(空格)對。

寫法一:

desired_caps:

platformname: android

devicename: xiaomi_mix

platformversion:

5.1 unicodekeyboard:

true

resetkeyboard:

true

noreset:

true

automationname:uiautomator2

ip: localhost

port:

4723

寫法二:

desired_caps:

用一組橫行破折號(-)開頭

desired_caps:

-platformname: android

-platformversion:

7.1.2

desired_caps:[,

]

用python讀取yaml檔案案例如下,先用open方法讀取檔案資料,再通過load方法轉成字典,這個load跟json裡面的load是相似的。

import yaml

class

readconfig

:def

__init__

(self, yaml_file)

: self.yaml_file = yaml_file

"""用python讀取yaml檔案案例如下,先用open方法讀取檔案資料,再通過load方法轉成字典,

這個load跟json裡面的load是相似的。

# coding:utf-8

"""def

read_yaml

(self)

:# open方法開啟直接讀出來,

with

open

(self.yaml_file, encoding=

'utf-8'

)as f:

# f.read()此處讀出來是字串,用load方法轉字典

msg = yaml.load(stream=f.read(

),loader=yaml.fullloader)

print

(msg)

defwrite_yaml

(self)

:pass

if __name__ ==

'__main__'

:# 例項化物件,獲取當前指令碼所在資料夾路徑

rc = readconfig(

) rc.read_yaml(

)

推薦文章

python筆記14-讀取yaml配置檔案(pyyaml)

python讀取YAML檔案步驟

安裝 pyyaml 模組 pip install pyyaml 編寫 存在 test.yml 檔案,內容如下 name xiaohong age 24 books 吶喊 朝花夕拾讀取yaml 如下 import yaml file open test.yaml r encoding utf 8 使用...

python筆記 讀取yaml檔案

使用yaml.load 總是出現告警 yamlloadwarning calling yaml.load without loader is deprecated,as the default loader is unsafe.please read for full details.原因 yaml...

python讀取配置檔案yaml

yaml 一種非標記語言,可以簡單表達清單 列表等資料形態,檔案字尾為 yml 1.大小寫敏感 2.使用縮排表示層級關係 3.縮排不允許使用tab,只允許空格,但是對空格數不要求 4.表示注釋 key value 和value之間應該有空格 字典 name test age 12 列表 1,2 3 ...