欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python讀寫(xiě)yaml文件

 更新時(shí)間:2022年03月20日 10:38:34   作者:殷殷殷先森丶  
這篇文章主要介紹了Python讀寫(xiě)yaml文件,yaml?是專(zhuān)門(mén)用來(lái)寫(xiě)配置文件的語(yǔ)言,非常簡(jiǎn)潔和強(qiáng)大,之前用ini也能寫(xiě)配置文件,有點(diǎn)類(lèi)似于json格式,下面關(guān)于Python讀寫(xiě)yaml文件的詳細(xì)資料,需要的小伙伴可以參考一下

1.關(guān)于yaml

yaml基本語(yǔ)法規(guī)則:

  • 大小寫(xiě)敏感
  • 使用縮進(jìn)表示層級(jí)關(guān)系
  • 縮進(jìn)時(shí)不允許使用Tab鍵,只允許使用空格。
  • 縮進(jìn)的空格數(shù)目不重要,只要相同層級(jí)的元素左側(cè)對(duì)齊即可
  • #表示注釋?zhuān)瑥倪@個(gè)字符一直到行尾,都會(huì)被解析器忽略,這個(gè)和python的注釋一樣

2.yaml數(shù)據(jù)結(jié)構(gòu)

YAML 支持的數(shù)據(jù)結(jié)構(gòu)有三種:

  • 對(duì)象

鍵值對(duì)的集合,又稱(chēng)為映射(mapping)/ 哈希(hashes) / 字典(dictionary)
對(duì)象的一組鍵值對(duì),使用冒號(hào)結(jié)構(gòu)表示。

  • 數(shù)組

一組按次序排列的值,又稱(chēng)為序列(sequence) / 列表(list)
一組連詞線開(kāi)頭的行,構(gòu)成一個(gè)數(shù)組。

  • 純量(scalars

單個(gè)的、不可再分的值
包括字符串,布爾值,整數(shù),浮點(diǎn)數(shù),Null,時(shí)間,日期

3.yaml文件格式

auth.login:
? data:
? ? name: '18888888883'
? ? password: jnyj123456
? url: https://XXXX-api-XXXX.zje.com/auth/login
headers:
? Accept: '*/*'
? Accept-Encoding: gzip, deflate, br
? Accept-Language: zh-CN,zh;q=0.9
? Connection: keep-alive
? Content-Length: '46'
? Content-type: application/json
? Host: dexin-api-test.zje.com
? Origin: https://XXXX-spa-XXX.zje.com
? Referer: https://XXXX-spa-XXX.zje.com/
? Sec-Fetch-Dest: empty
? Sec-Fetch-Mode: cors
? Sec-Fetch-Site: same-site
? User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML,
? ? like Gecko) Chrome/98.0.4758.80 Safari/537.36
? authorization: Bearer
? sec-ch-ua: '" Not A;Brand";v="33"
? sec-ch-ua-mobile: ?0000
? sec-ch-ua-platform: macOSis

學(xué)習(xí)產(chǎn)出:

class OpenYaml(object):

? ? def __init__(self):
? ? ? ? self.file_path = os.path.join(route("/DataYaml/yaml.yaml")) ?# 拼接讀取的文件路徑

? ? def open(self, *args):
? ? ? ? '''
? ? ? ? ? ?args[0]: 字典名稱(chēng)
? ? ? ? ? ?args[1]: 字段值
? ? ? ? ? ?讀取文件
? ? ? ? '''
? ? ? ? try:
? ? ? ? ? ? if len(args) == 2: ?# 根據(jù)傳值判斷執(zhí)行內(nèi)容
? ? ? ? ? ? ? ? with open(self.file_path, "r") as f: ?# 讀取yaml
? ? ? ? ? ? ? ? ? ? Json = f.read() ?# 獲取yaml
? ? ? ? ? ? ? ? ? ? Dict = yaml.safe_load(Json)[args[0]] ?# 提取制定內(nèi)容
? ? ? ? ? ? ? ? if args[1] in Dict.keys(): ?# 判斷key是否存在
? ? ? ? ? ? ? ? ? ? logs.info(f"yaml文件,查找內(nèi)容成功,內(nèi)容:{Dict[args[1]]}")
? ? ? ? ? ? ? ? ? ? return Dict[args[1]]
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print(f"對(duì)應(yīng)字段{args[1]}不存在...")
? ? ? ? ? ? ? ? ? ? logs.info(f"對(duì)應(yīng)字段{args[1]}不存在...")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? with open(self.file_path, "r") as f:
? ? ? ? ? ? ? ? ? ? Json = f.read()
? ? ? ? ? ? ? ? ? ? Dict = yaml.safe_load(Json)[args[0]]
? ? ? ? ? ? ? ? return Dict
? ? ? ? except Exception as e:
? ? ? ? ? ? print(f'讀取yaml文件,報(bào)錯(cuò):{e}')
? ? ? ? ? ? logs.info(f'讀取yaml文件,報(bào)錯(cuò):{e}')

? ? def Wri_file(self, *args):

? ? ? ? '''
? ? ? ? :param args: args[0] 接口字段、args[1] key、 args[2] value
? ? ? ? :return: None
? ? ? ? 把字段寫(xiě)入yaml
? ? ? ? '''
? ? ? ? try:
? ? ? ? ? ? with open(self.file_path, encoding="utf-8") as f: ?# 讀取文件
? ? ? ? ? ? ? ? data = yaml.load(f.read(), Loader=yaml.FullLoader) ?# 獲取讀取內(nèi)容
? ? ? ? ? ? print(data[args[0]])
? ? ? ? ? ? if data is not None: ?# 判斷讀取內(nèi)容是否為空
? ? ? ? ? ? ? ? if str(data[args[0]][args[1]]) in str(data[args[0]]): ?# 判斷name是否存在在dict
? ? ? ? ? ? ? ? ? ? data[args[0]][args[1]] = args[2]
? ? ? ? ? ? ? ? ? ? with open(self.file_path, 'w', encoding="utf-8") as f: ?# 寫(xiě)入
? ? ? ? ? ? ? ? ? ? ? ? yaml.dump(data, stream=f, allow_unicode=True)
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print("寫(xiě)入文件的字段不存在!寫(xiě)入失敗...")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? logs.info("寫(xiě)入文件的返回值為空!不能寫(xiě)入...")
? ? ? ? except Exception as y:
? ? ? ? ? ? logs.info(f"寫(xiě)入文件失?。簕y}")


if __name__ == "__main__":
? ? OpenYaml().Wri_file("headers", "Content-Length", "22")
?? ?OpenYaml().open("auth.login", "data")

到此這篇關(guān)于Python讀寫(xiě)yaml文件的文章就介紹到這了,更多相關(guān)Python讀寫(xiě)yaml內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論