首页 > 编程语言 >Python之ruamel.yaml模块详解(一)

Python之ruamel.yaml模块详解(一)

时间:2023-02-09 14:00:41浏览次数:56  
标签:None ruamel stream YAML Python self yaml

(Python之ruamel.yaml模块详解(一))

1 ruamel.yaml简介

  • ruamel.yaml是一个yaml解析器;
  • ruamel.yaml是一个用于Pythonyaml1.2加载器/转储程序包;
  • 它是PyYAML 3.11的衍生产品;
  • ruamel.yaml库继承子PyMYAL库,读写方法基本相同,目前来说可以根据自己的习惯选择使用 ruamel.yaml 还是 PyMYAL 进行yaml文件的读写操作。

2 ruamel.yaml安装

  • 前提条件是:确保安装了最新版本的pipsetuptools(>=20.6.8)

2.1 setuptools安装

pip install -U pip setuptools wheel

2.2 pip安装ruamel.yaml

  • 一般情况安装到这就可以了,后续的2.3和2.4仅供参考使用。
pip install ruamel.yaml
C:\Users\Administrator>pip install ruamel.yaml
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting ruamel.yaml
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9e/cb/938214ac358fbef7058343b3765c79a1b7ed0c366f7f992ce7ff38335652/ruamel.yaml-0.17.21-py3-none-any.whl (109 kB)
     --------------------------------------- 109.5/109.5 kB 2.1 MB/s eta 0:00:00

Collecting ruamel.yaml.clib>=0.2.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/da/f4/928e950925fe1b9eb048ddab8eef073a52e9ae01afd06f98f1daf743e355/ruamel.yaml.clib-0.2.7-cp37-cp37m-win_amd64.whl (115 kB)
     ------------------------------------- 115.9/115.9 kB 357.0 kB/s eta 0:00:00

Installing collected packages: ruamel.yaml.clib, ruamel.yaml
Successfully installed ruamel.yaml-0.17.21 ruamel.yaml.clib-0.2.7

2.3 处理jinja2/YAML模板

pip install ruamel.yaml[jinja2]

2.4 yaml命令行实用程序

pip install ruamel.yaml.cmd

3 yaml.load()和yaml.dump()解析

3.1 yaml.load()读ymal文件

from ruamel.yaml import YAML

yaml=YAML(typ='safe')   
yaml.load(doc)
  • 以上typ若没有指定,默认为 'rt' (round-trip)
  • doc可以是文件指针(即具有.read()方法、字符串或pathlib.Path()的对象);
  • typ='safe'完成了与safe_load()之前相同的操作:加载文档而不解析未知标记;
  • pure=True以使用纯Python实现强制执行,否则将在可能/可用时使用更快的C库。
  • 详细的可以参考源码:
class YAML:
    def __init__(self, *, typ=None, pure=False, output=None, plug_ins=None):  # input=None,
        # type: (Any, Optional[Text], Any, Any, Any) -> None
        """
        typ: 'rt'/None -> RoundTripLoader/RoundTripDumper,  (default)
             'safe'    -> SafeLoader/SafeDumper,
             'unsafe'  -> normal/unsafe Loader/Dumper
             'base'    -> baseloader
        pure: if True only use Python modules
        input/output: needed to work as context manager
        plug_ins: a list of plug-in files
        """
    def load(self, stream):
        # type: (Union[Path, StreamTextType]) -> Any
        """
        at this point you either have the non-pure Parser (which has its own reader and
        scanner) or you have the pure Parser.
        If the pure Parser is set, then set the Reader and Scanner, if not already set.
        If either the Scanner or Reader are set, you cannot use the non-pure Parser,
            so reset it to the pure parser and set the Reader resp. Scanner if necessary
        """
        if not hasattr(stream, 'read') and hasattr(stream, 'open'):
            # pathlib.Path() instance
            with stream.open('rb') as fp:
                return self.load(fp)
        constructor, parser = self.get_constructor_parser(stream)
        try:
            return constructor.get_single_data()
        finally:
            parser.dispose()
            try:
                self._reader.reset_reader()
            except AttributeError:
                pass
            try:
                self._scanner.reset_scanner()
            except AttributeError:
                pass

3.2 yaml.dump()写ymal文件

from ruamel.yaml import YAML

yaml=YAML()
yaml.default_flow_style = False
yaml.dump({'a': [1, 2]}, s)
  • 方式和load差不多;
  • s可以是文件指针(即,具有.write()方法的对象,或pathlib.Path()。如果要显示输出,只需sys.stdout即可;
  • 如果需要转换输出的字符串表示形式,请提供一个将字符串作为输入并返回一个字符串的函数:
def tr(s):
    return s.replace('\n', '<\n')  # such output is not valid YAML!

yaml.dump(data, sys.stdout, transform=tr)
  • 详细查看源码:
 def dump(self, data, stream=None, *, transform=None):
        # type: (Any, Union[Path, StreamType], Any, Any) -> Any
        if self._context_manager:
            if not self._output:
                raise TypeError('Missing output stream while dumping from context manager')
            if transform is not None:
                raise TypeError(
                    '{}.dump() in the context manager cannot have transform keyword '
                    ''.format(self.__class__.__name__)
                )
            self._context_manager.dump(data)
        else:  # old style
            if stream is None:
                raise TypeError('Need a stream argument when not dumping from context manager')
            return self.dump_all([data], stream, transform=transform)

3.3 基于C的SafeLoader

from ruamel.yaml import YAML

yaml=YAML(typ="safe")
yaml.load("""a:\n  b: 2\n  c: 3\n""")

3.4 基于Python的SafeLoader

from ruamel.yaml import YAML

yaml=YAML(typ="safe", pure=True)
yaml.load("""a:\n  b: 2\n  c: 3\n""")

标签:None,ruamel,stream,YAML,Python,self,yaml
From: https://blog.51cto.com/NoamaNelson/6046911

相关文章

  • python多维数组的每列的最值
    python代码实现importnumpyasnpdefmaxmin(array):#求每列的最值maxlist=[]minlist=[]foriinrange(len(array[0])):#行数col=[]......
  • python3 时间戳转换
    importtimedeftime_conversion(times):#转换成新的时间格式(2016-05-0520:28:54)dt=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime(times))......
  • Python 分支结构
    阅读目录​​应用场景​​​​if语句的使用​​​​例子1:英制单位英寸与公制单位厘米互换​​​​例子2:百分制成绩转换为等级制成绩​​​​例子3:输入三条边长,如果能构成三......
  • Python爬虫爬取html中div下的多个class标签并存入数据库
    使用python爬虫爬取html页面div中的多个class标签,获取后将数据存成列表,然后存入数据库importmysql.connectorimportpymysqlimportrequestsfrombs4importBeautif......
  • python开发笔记--ImportError: cannot import name 'sysconfig' from 'distutils' (/u
    异常情况:ubuntu20.4安装python3.10和pip后,执行piplist提示如下:ImportError:cannotimportname'sysconfig'from'distutils'(/usr/lib/python3.8/distutils/__init__......
  • Vitualenvwrapper: 管理 python的 虚拟环境
            linux:install  Vitualenvwrapper[user]-3D05SQ3:~/python/managing-python-packages-virtual-environments/flask/venv$python3-mpipi......
  • 1、python_批量检测端口号
    #!/usr/bin/envpython#coding:utf-8#Author:zikangimportsocketlist_str='''172.31.7.1038080172.31.7.1046379172.31.7.1053306'''OK_list=[]Timeo......
  • 第04天-python函数与高阶函数
    1、函数和传实参1.1、Python函数1.1.1、函数的三分类数学定义y=f(x),y是x的函数,x是自变量。y=f(x0,x1,...,xn)Python函数由若干语句组成的语句块、函数名称、参......
  • 第02天-python线性数据结构
    1、数值处理1.1、内建常用数据类型1.1.1、分类数值型int、float、complex、bool序列sequence字符串str、字节序列bytes、bytearray列表list、元组t......
  • 第03天-python字节序列字典
    1、字节序列和切片1.1、字节序列Python3引入两个新的类型bytes、bytearray。bytes不可变字节序列;bytearray是可变字节数组。字节的世界里面没有编码1.2、编码与解......