Python实现读取Properties配置文件的方法
本文实例讲述了Python实现读取Properties配置文件的方法。分享给大家供大家参考,具体如下:
JAVA本身提供了对于Properties文件操作的类,项目中的很多配置信息都是放在了Properties文件。但是Python并没有提供操作Properties文件的库,所以,自己动手写个一个可以加载Properties文件的脚本。
class Properties:
  fileName = ''
  def __init__(self, fileName):
    self.fileName = fileName
  def getProperties(self):
  try:
  pro_file = open(self.fileName, 'r')
    properties = {}
    for line in pro_file:
      if line.find('=') > 0:
        strs = line.replace('\n', '').split('=')
        properties[strs[0]] = strs[1]
  except Exception, e:
  raise e
  else:
  pro_file.close()
    return properties
实际调用:
fileName = sys.path[0] + '\\'+ 'system.properties' p = Properties(fileName) properties = p.getProperties() print properties[Key]
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
您可能感兴趣的文章:
- Python读取properties配置文件操作示例
 - Python读写配置文件的方法
 - Python中使用ConfigParser解析ini配置文件实例
 - python实现的解析crontab配置文件代码
 - python用ConfigObj读写配置文件的实现代码
 - Python使用自带的ConfigParser模块读写ini配置文件
 - Python配置文件解析模块ConfigParser使用实例
 - python读写ini配置文件方法实例分析
 - Python实现配置文件备份的方法
 - Python自动化测试ConfigParser模块读写配置文件
 - Python实现加载及解析properties配置文件的方法
 
 赞 (0)
                        