Python读取Properties配置文件 Python实现读取Properties配置文件的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python读取Properties配置文件 Python实现读取Properties配置文件的方法

Robin_宾宾   2021-03-28 我要评论
想了解Python实现读取Properties配置文件的方法的相关内容吗,Robin_宾宾在本文为您仔细讲解Python读取Properties配置文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们