如何解析配置文件

如何解析配置文件
什么是配置文件?
在计算机科学领域,配置文件(英语: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": "[email protected]"
    },
    "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"
              }
            }
          

你可能感兴趣的:(python,python,json)