python工具封装:读写配置文件config

2021-01-14 14:42:54 浏览数 (1)

读写配置文件

代码语言:javascript复制
class Properties(object):

    def __init__(self, file_name):
        self.file_name = file_name
        self.properties = {}

    def _get_dict(self, str_name, dict_name, value):
        if (str_name.find('.') > 0):
            k = str_name.split('.')[0]
            dict_name.setdefault(k, {})
            return self._get_dict(str_name[len(k)   1:], dict_name[k], value)
        else:
            # print(value)
            # self.properties  = str_name   "="   value   ","
            dict_name[str_name] = value
            return

    def get_properties(self):
        try:
            pro_file = open(self.file_name, 'Ur', encoding='utf-8')
            for line in pro_file.readlines():
                line = line.strip().replace('n', '')
                if line.find("#") != -1:
                    line = li

0 人点赞