日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 第三方模块 yaml - 处理 YAML (专门用来写配置文件的语言)

發布時間:2023/12/20 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 第三方模块 yaml - 处理 YAML (专门用来写配置文件的语言) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

markdown 的配置使用 Yaml —— Yet Another Markup Language :另一種標記語言。

簡介

YAML 是專門用來寫配置文件的語言,非常簡潔和強大,遠比 JSON 格式方便。
YAML在python語言中有PyYAML安裝包。
YAML 語言(發音 /?j?m?l/ )的設計目標,就是方便人類讀寫。它實質上是一種通用的數據串行化格式。

它的基本語法規則如下:

  • 大小寫敏感
  • 使用縮進表示層級關系
  • 縮進時不允許使用Tab鍵,只允許使用空格。
  • 縮進的空格數目不重要,只要相同層級的元素左側對齊即可
  • # 表示注釋,從這個字符一直到行尾,都會被解析器忽略,這個和python的注釋一樣
  • YAML 支持的數據結構有三種:

  • 對象:鍵值對的集合。鍵值對用冒號 “:” 結構表示,冒號與值之間需用空格分隔
  • 數組:一組按次序排列的值。數組前加有 “-” 符號,符號與值之間需用空格分隔
  • 純量(scalars):單個的、不可再分的值。字符串、布爾值、整數、浮點數、Null、時間、日期
  • python 操作 YAML 文件的模塊 pyyaml

  • pyyaml 庫不支持讀取文檔中嵌入的yaml。這是一個提取yaml文本的實用程序函數,因此可以先提取 yaml 的數據對其進行解析
  • 1. 安裝

    pip install pyyaml

    2. yaml 文件示例

    apiVersion: apps/v1 kind: Deployment metadata:name: linux-node02namespace: yaml-demo spec:replicas: 1selector:matchLabels:app: podinfotemplate:metadata:labels:app: podinfospec:containers:- image: quay.io/stefanprodan/podinfo:0.3.0name: podinfodports:- containerPort: 9898

    3. 讀取 yaml 文件

    import yaml import osyamlPath = 'config.yaml' with open(yamlPath,'r',encoding='utf-8') as f:# print(f.read())result = f.read()x = yaml.load(result,Loader=yaml.FullLoader)print(type(x))print(x) <class 'dict'> {'apiVersion': 'apps/v1', 'kind': 'Deployment', 'metadata': {'name': 'linux-node02', 'namespace': 'yaml-demo'}, 'spec': {'replicas': 1, 'selector': {'matchLabels': {'app': 'podinfo'}}, 'template': {'metadata': {'labels': {'app': 'podinfo'}}, 'spec': {'containers': [{'image': 'quay.io/stefanprodan/podinfo:0.3.0', 'name': 'podinfod', 'ports': [{'containerPort': 9898}]}]}}}}

    備注:如果報警告 YAMLLoadWarning: calling yaml.load() without Loader=… is deprecated

    修改代碼如下:

    import yaml from Common.dir_config import *fs = open(os.path.join(caps_dir, "data.yaml"),encoding="UTF-8") datas = yaml.load(fs,Loader=yaml.FullLoader) #添加后就不警告了

    3.1 分段yaml文件中多個文檔

  • 多個文檔在一個yaml文件,使用 — 分隔方式來分段,示例如下
  • --- animal1: dog age: 2 --- animal2: cat age: 3
  • python腳本讀取一個yaml文件中多個文檔方法
  • python獲取yaml數據時需使用 load_all() 函數來解析全部的文檔,再從中讀取對象中的數據,load_all() 返回一個生成器

    def get_yaml_load_all(yaml_file):# 打開yaml文件file = open(yaml_file, 'r', encoding="utf-8")file_data = file.read()file.close()all_data = yaml.load_all(file_data)for data in all_data:print(data)current_path = os.path.abspath(".") yaml_path = os.path.join(current_path, "config.yaml") get_yaml_load_all(yaml_path) """結果 {'animal1': 'dog', 'age': 2} {'animal2': 'cat', 'age': 3} """

    4. 修改 yaml 文件

    import yaml import osyamlPath = 'config.yaml' # 修改yaml配置 with open(yamlPath,'r',encoding='utf-8') as f:# print(f.read())result = f.read()x = yaml.load(result,Loader=yaml.FullLoader)# 修改x['metadata']['name'] = 'linux-node02'with open(yamlPath,'w',encoding='utf-8') as w_f:# sort_keys=False,寫入yaml的數據則不會排序后寫入# allow_unicode 防止中文轉義yaml.dump(x, w_f, allow_unicode=True, sort_keys=False)

    總結

    以上是生活随笔為你收集整理的python 第三方模块 yaml - 处理 YAML (专门用来写配置文件的语言)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。