Python配置文件-yaml格式

import yaml

class ReadYaml():

    def yaml_read(FilePath):
        """
        读取yaml中的数据
        :return:
        """
        fileNamePath = FilePath
        fr = open(fileNamePath,'r',encoding='utf-8')
        cont = fr.read()
        x = yaml.load(cont)
        fr.close()
        return x

    def yaml_update(FilePath,data):
        #写入yaml
        fileNamePath = FilePath
        fr = open(fileNamePath, 'w', encoding='utf-8')
        yaml.dump(data,fr)
        fr.close()


if __name__ == '__main__':
    #调用方法
    data = {
        "token": "123654785",
        "refreshToken":"fsdf4sdf4545435",
        "expiredTime":"11111111"
    }
    ReadYaml.yaml_update("E:\python\\config\\token.yaml",data)
    x = ReadYaml.yaml_read("E:\python\\config\\token.yaml")
    print(x["token"])

操作配置文件

转载于:https://www.cnblogs.com/siyz/p/11116776.html

你可能感兴趣的:(Python配置文件-yaml格式)