代码改变世界

Python配置文件-yaml格式

2019-07-01 21:18  白天很白≯  阅读(207)  评论(0编辑  收藏
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"])

操作配置文件