Python-读取yaml文件(响应数据报告模板)
发布日期:2021-05-10 11:26:51 浏览次数:18 分类:精选文章

本文共 2445 字,大约阅读时间需要 8 分钟。

������������

������������������������������������������������������������������������������������

pip install pyyaml

YAML ������

YAML ������

  • YAML������������������������������������������tab���������������������������������������������������������������������

  • ���������������������������������

  • ���������������������������������������
  • key: value
    1. ������������������������������ {} ������������������������������������������
    2. key:
      subkey: subvalue
      1. ������������������������������ - ������������������������������������������������
      2. - item1
        - item2
        • ������������������ # ���������
        # ������������������
        key: value

YAML ������

������������������������������������������������������������������������������������

HOST:   {    'host':'http://v.juhe.cn',}
PATH: { 'path':'/toutiao/index',}
EXPECT: { 'expect':'success!',}
HttpApi: { "retcode": 200, "msg": "\u67e5\u8be2\u6210\u529f", "retlist": [
{ "id": 3, "method": 2, "path": "/api/test2", "data": "{\"user\":\"test\",\"psw\":\"123123\"}", "content_type": 1, "headers": "{\"Cache-Control\":\"no-cache\"}", "auth_type": 1, "desc": "\u6d4b\u8bd5\u63a5\u53e32", "create_time": "2021-03-30/07:08", "update_time": "2021-03-30/07:37", "module": {
"id": 3, "desc": "login", "name": "\u767b\u5f55\u6a21\u5757"
}
] }

������ YAML ������

��������� Python ��������������������� yaml ��������������������� YAML ���������������������������������

  • ������������������
  • import yaml
    1. ������������ YAML ������������������������������������������������������������
    2. def read_yml(path, key):
      with open(path, encoding='utf-8') as f:
      data_json = f.read()
      res = yaml.safe_load(data_json)
      return res[key]
      1. ���������������������������������
      2. req = read_yml('conf.yml', 'HOST')
        print(req)

        ������������

        ������������������������������������������ YAML ���������������������������������������������������������������������

        ������������������������

        ������������������������������������������������������ YAML ���������

      3. ������������������������������������������������������������ YAML ������������������������������������������

      4. ���������������������������������������������������������������������������������������������������������������������������

      5. ������������������������������������������������������������������������������������

    上一篇:Docker基础+Docker安装mysql
    下一篇:Python-类方法、实例方法、对象方法区别

    发表评论

    最新留言

    路过按个爪印,很不错,赞一个!
    [***.219.124.196]2025年04月01日 09时12分19秒