python读取配置文件创建字典

配置文件天然具有哈希表特性。

在python里面可以使用字典来替代哈希表。

程序工作流程是,在运行前读取配置文件获取内容,再根据内容进行数据处理。


环境:

    Python3.6.5

    配置文件 config.txt 


配置文件config.txt的内容是以 “=”相连key和value,每行一对kv。


核心代码:

    for i in config_file:

        i = i.split("\n")      #去掉换行符

        k = i.split("=")[0]

        v = i.split("=")[1]

        dict_temp[k] = v


分割线==============================================

示例代码:

def readconfig(path):

    config_file = open(path)

    dict_temp = {}

    try:

        for line in config_file:

            line = line.split("\n")      #去掉换行符

            k = line.split("=")[0]

            v = line.split("=")[1]

            dict_temp[k] = v

    finally:

            config_file.close()

    return dict_temp



原创内容,抄袭必究

---20190806

---David.Ocean

你可能感兴趣的:(python读取配置文件创建字典)