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

作者:Robin_宾宾 时间:2021-01-05 17:28:21 

本文实例讲述了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程序设计有所帮助。

来源:http://blog.sina.com.cn/s/blog_6a24f10901018lhn.html

标签:Python,Properties,配置文件
0
投稿

猜你喜欢

  • go sync Once实现原理示例解析

    2023-07-01 12:21:13
  • 谈谈如何管理门户级网站的CSS/IMG/JS文件

    2009-09-03 11:48:00
  • 深入研究ASP中的Session

    2007-09-13 12:37:00
  • PHPStudy下如何为Apache安装SSL证书的方法步骤

    2023-11-14 18:57:09
  • 交互设计中的“007功能”

    2007-10-29 11:53:00
  • asp什么情况下用响应缓冲会提高运行速度?

    2010-07-14 21:02:00
  • SQL语句之WHERE子句的使用方法

    2007-08-11 12:25:00
  • 使用Protocol Buffers的C语言拓展提速Python程序的示例

    2022-12-04 08:46:20
  • 用Dreamweaver实现飘浮光球特效

    2008-03-03 12:28:00
  • 基于Python实现定时自动给微信好友发送天气预报

    2023-09-13 01:24:05
  • asp+ajax仿google搜索提示效果代码

    2011-03-09 10:52:00
  • 窥探jQuery——面向JavaScript程序员

    2008-06-17 14:35:00
  • es6函数之严格模式用法实例分析

    2023-08-09 06:15:40
  • PHP中的一些常用函数收集

    2023-10-09 02:06:06
  • python处理文本文件实现生成指定格式文件的方法

    2022-01-07 21:41:43
  • PHP文件运行步骤以及数据库的连接代码

    2023-05-25 08:32:51
  • 客户认同的就是好商品

    2009-08-31 16:41:00
  • Google中秋logo被恶搞

    2007-09-25 15:55:00
  • Python实现读取文件夹按数字排序功能

    2023-08-24 02:12:45
  • sql server中批量插入与更新两种解决方案分享(存储过程)

    2012-05-22 18:29:59
  • asp之家 网络编程 m.aspxhome.com