• python3.7 120prop- 读写.properties文件


    120prop-python3.7 读写.properties文件

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import re
    import os
    import tempfile
    
    
    class Properties:
    
        def __init__(self, file_name):
            self.file_name = file_name
            self.properties = {}
            try:
                fopen = open(self.file_name, 'r')
                for line in fopen:
                    line = line.strip()
                    if line.find('=') > 0 and not line.startswith('#'):
                        strs = line.split('=')
                        self.properties[strs[0].strip()] = strs[1].strip()
            except Exception :
                raise e
            else:
                fopen.close()
    
        def has_key(self, key):
            return key in self.properties
    
        def get(self, key, default_value=''):
            if key in self.properties:
                return self.properties[key]
            return default_value
    
        def put(self, key, value):
            self.properties[key] = value
            replace_property(self.file_name, key + '=.*', key + '=' + value, True)
    
    
    def replace_property(file_name, from_regex, to_str, append_on_not_exists=True):
        tmpfile = tempfile.TemporaryFile()
    
        if os.path.exists(file_name):
            r_open = open(file_name, 'r')
            pattern = re.compile(r'' + from_regex)
            found = None
            for line in r_open:
                if pattern.search(line) and not line.strip().startswith('#'):
                    found = True
                    line = re.sub(from_regex, to_str, line)
                tmpfile.write(line.encode())
            if not found and append_on_not_exists:
                tmpfile.write(('
    ' + to_str).encode())
            r_open.close()
            tmpfile.seek(0)
    
            content = tmpfile.read()
    
            if os.path.exists(file_name):
                os.remove(file_name)
    
            w_open = open(file_name, 'wb')
            w_open.write(content)
            w_open.close()
    
            tmpfile.close()
        else:
            print ("file %s not found" % file_name)
    
    
    if __name__ == "__main__":
        file_path = 'jdbc.properties'
        props = Properties(file_path)  # 读取文件
        props.put('jdbc.url', 'value_a')  # 修改/添加key=value
        print (props.get('key_a'))  # 根据key读取value
        print ("props.has_key('key_a')=" + str(props.has_key('key_a'))) # 判断是否包含该key
  • 相关阅读:
    java Android get date before 7 days (one week) Stack Overflow
    计算机网络与分布式系统实验室 北京大学
    得到IFrame中的Document
    eclipse如何把多个项目放在一个文件夹下
    windows 32位程序编译成64位
    iPhone5和iOS6上HTML5开发的新增功能
    Thinking in Java之接口回调改版
    Java学习笔记35:Java常用字符串操作函数
    进一步优化Bitmap Cache策略
    微软安全新闻聚焦双周刊第三十期
  • 原文地址:https://www.cnblogs.com/xinxihua/p/12615296.html
Copyright © 2020-2023  润新知