python读取Java的property配置文件-自用

因为前段时间,工作需要大量处理Java的Properties文件,需要读取里面的数据整理成Excel表格,相对于Java读取Properties是很轻松的,自己处理的时候也是使用的Java来处理并整理成Excel,但是有的同事就不是很擅长使用Java,他们更加擅长使用Python,但我发现Python并没有读取Python的类库,网上Python读取Properties文件的代码也太冗长,当然可能更安全...(由于自己并没有去看过).我就简单使用Python模仿了Java的Properties类仿造写了部分功能.使得Python读取Properties更加的方便

代码如下.

class properties:

    content = {}

    def load(self,path: str):
        with open(path,'r') as lines:
            for line in lines:
                if len(line) == 1:
                    continue
                if line[0:1] == '#':
                    continue
                strs = line.split("=")
                self.content[strs[0]] = strs[1].strip()


    def get(self,key):
        if key not in self.content:
            return 'null'
        return self.content[key]

    def keys(self):
        return self.content.keys()






if __name__ == '__main__':
    pro = properties()
    pro.load("./Import_TMIS_Report_YEITF.properties")
    print(pro.get("name"))
    print(pro.get("extSystemName"))

之后如果有时间会继续完善这个Python工具.

posted @ 2021-02-19 23:33  宇。  阅读(289)  评论(0)    收藏  举报