如何解析配置文件

配置文件在计算机科学中用于设置程序参数。常见的格式包括JSON、INI和YAML。JSON因其类似Python字典的结构而便于解析,可以使用`json_parse.py`进行操作。同样,可以读写INI配置文件,如`conf.ini`,以及解析YAML文件,如通过`yaml_parse.py`完成。
摘要由CSDN通过智能技术生成

如何解析配置文件
什么是配置文件?
在计算机科学领域,配置文件(英语:configuration file)是一种计算机文件,可以为一些计算机程序配置参数和初始设置。

一个项目中,为什么要设置一个配置文件呢?
方便修改
参数设置和逻辑代码分离(解耦)(高内聚,低耦合)

常见的配置文件格式:
aaa.ini
xxx.xml
xxxxx.json
xxfada.yaml

什么是json
文本、字符串、有固定的格式,格式长得像python中字典和列表的组合

如何解析json文件
swagger.json

{
   
  "swagger": "2.0",
  "info": {
   
    "description": "This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.",
    "version": "1.0.5",
    "title": "Swagger Petstore",
    "termsOfService": "http://swagger.io/terms/",
    "contact": {
   
      "email": "apiteam@swagger.io"
    },
    "license": {
   
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "host": "petstore.swagger.io",
  "basePath": "/v2",
  "tags": [
    {
   
      "name": "pet",
      "description": "Everything about your Pets",
      "externalDocs": {
   
        "description": "Find out more",
        "url": "http://swagger.io"
      }
    },
    {
   
      "name": "store",
      "description": "Access to Petstore orders"
    },
    {
   
      "name": "user",
      "description": "Operations about user",
      "externalDocs": {
   
        "description": "Find out more about our store",
        "url": "http://swagger.io"
      }
    }
  ],
  "schemes": [
    "https",
    "http"
  ],
  "paths": {
   
    "/pet/{petId}/uploadImage": {
   
      "post": {
   
        "tags": [
          "pet"
        ],
        "summary": "uploads an image",
        "description": "",
        "operationId": "uploadFile",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
   
            "name": "petId",
            "in": "path",
            "description": "ID of pet to update",
            "required": true,
            "type": "integer",
            "format": "int64"
          },
          {
   
            "name": "additionalMetadata",
            "in": "formData",
            "description": "Additional data to pass to server",
            "required": false,
            "type": "string"
          },
          {
   
            "name": "file",
            "in": "formData",
            "description": "file to upload",
            "required": false,
            "type": "file"
          }
        ],
        "responses": {
   
          "200": {
   
            "description": "successful operation",
            "schema": {
   
              "$ref": "#/definitions/ApiResponse"
            }
          }
        },
        "security": [
          {
   
            "petstore_auth": [
              "write:pets",
              "read:pets"
            ]
          }
        ]
      }
    },
    "/pet": {
   
      "post": {
   
        "tags": [
          "pet"
        ],
        "summary": "Add a new pet to the store",
        "description": "",
        "operationId": "addPet",
        "consumes": [
          "application/json",
          "application/xml"
        ],
        "produces": [
          "application/json",
          "application/xml"
        ],
        "parameters": [
          {
   
            "in": "body",
            "name": "body",
            "description": "Pet object that needs to be added to the store",
            "required": true,
            "schema": {
   
              "$ref": "#/definitions/Pet"
            }
          }
        ],
        "responses": {
   
          "405": {
   
            "description": "Invalid input"
          }
        },
        "security": [
          {
   
            "petstore_auth": [
              "write:pets",
              "read:pets"
            ]
          }
        ]
      },
      "put": {
   
        "tags": [
          "pet"
        ],
        "summary": "Update an existing pet",
        "description": "",
        "operationId": "updatePet",
        "consumes": [
          "application/json",
          "application/xml"
        ],
        "produces": [
          "application/json",
          "application/xml"
        ],
        "parameters": [
          {
   
            "in": "body",
            "name": "body",
            "description": "Pet object that needs to be added to the store",
            "required": true,
            "schema": {
   
              "$ref": "#/definitions/Pet"
            }
          }
        ],
        "responses": {
   
          "400": {
   
            "description": "Invalid ID supplied"
          },
          "404": {
   
            "description": "Pet not found"
          },
          "405": {
   
            "description": "Validation exception"
          }
        },
        "security": [
          {
   
            "petstore_auth": [
              "write:pets",
              "read:pets"
            ]
          }
        ]
      }
    },
    "/pet/findByStatus": {
   
      "get": {
   
        "tags": [
          "pet"
        ],
        "summary": "Finds Pets by status",
        "description": "Multiple status values can be provided with comma separated strings",
        "operationId": "findPetsByStatus",
        "produces": [
          "application/json",
          "application/xml"
        ],
        "parameters": [
          {
   
            "name": "status",
            "in": "query",
            "description": "Status values that need to be considered for filter",
            "required": true,
            "type": "array",
            "items": {
   
              "type": "string",
              "enum": [
                "available",
                "pending",
                "sold"
              ],
              "default": "available"
            },
            "collectionFormat": "multi"
          }
        ],
        "responses": {
   
          "200": {
   
            "description": "successful operation",
            "schema": {
   
              "type": "array",
              "items": {
   
                "$ref": "#/definitions/Pet"
              }
            }
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值