Python读取properties配置文件操作示例
作者:bobzhangshaobo 时间:2021-06-10 04:20:55
本文实例讲述了Python读取properties配置文件操作。分享给大家供大家参考,具体如下:
工作需要将Java项目的逻辑改为python执行,Java的很多配置文件都是.properties的,文件内容的格式是“键.键.键。。。=值”的格式例如A.B.C=value1,D.F=value2等。并且“#”用来注视。python没有专门处理properties格式的包,只有处理标准的ini格式的包。所以需要自己写一个python程序来处理。不说了上程序。
这里参考前面一篇:https://www.jb51.net/article/137390.htm
主要在前文的基础上,增加key.key.key=value的形式的支持
Util.py文件:
class Properties(object):
def __init__(self, fileName):
self.fileName = fileName
self.properties = {}
def __getDict(self,strName,dictName,value):
if(strName.find('.')>0):
k = strName.split('.')[0]
dictName.setdefault(k,{})
return self.__getDict(strName[len(k)+1:],dictName[k],value)
else:
dictName[strName] = value
return
def getProperties(self):
try:
pro_file = open(self.fileName, 'Ur')
for line in pro_file.readlines():
line = line.strip().replace('\n', '')
if line.find("#")!=-1:
line=line[0:line.find('#')]
if line.find('=') > 0:
strs = line.split('=')
strs[1]= line[len(strs[0])+1:]
self.__getDict(strs[0].strip(),self.properties,strs[1].strip())
except Exception, e:
raise e
else:
pro_file.close()
return self.properties
filename.properties文件:
a.b.d=v1
a.c=v2
d.e=v3
f=v4
测试文件text.py:
from Util import Properties
dictProperties=Properties("filename.properties").getProperties()
print dictProperties
输出:
{'a': {'c': 'v2', 'b': {'d': 'v1'}}, 'd': {'e': 'v3'}, 'f': 'v4'}
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/bobzhangshaobo/article/details/47617107
标签:Python,properties,配置文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
网页特效文字之—沙粒字
2023-07-06 07:32:10
![](https://img.aspxhome.com/file/UploadPic/20072/2007231168817s.jpg)
使用pycharm在本地开发并实时同步到服务器
2022-09-18 16:25:12
![](https://img.aspxhome.com/file/2023/2/103252_0s.png)
Python3将数据保存为txt文件的方法
2023-01-22 19:06:27
![](https://img.aspxhome.com/file/2023/0/75330_0s.png)
进一步理解Python中的函数编程
2023-12-07 16:50:16
python自动化之re模块详解
2021-07-01 19:34:16
PyCharm搭建一劳永逸的开发环境
2022-12-23 20:24:23
![](https://img.aspxhome.com/file/2023/7/107657_0s.jpg)
解密CSS Sprites:技巧、工具和教程
2011-01-11 19:38:00
![](https://img.aspxhome.com/file/UploadPic/20111/11/pokemon-29s.gif)
Python实现无损放大图片的示例代码
2022-12-15 12:50:12
![](https://img.aspxhome.com/file/2023/7/114677_0s.jpg)
详解用python实现简单的遗传算法
2022-03-21 15:16:34
![](https://img.aspxhome.com/file/2023/8/123698_0s.png)
减少新开窗口提升可访问性
2009-04-17 13:56:00
![](https://img.aspxhome.com/file/UploadPic/20094/17/msdn-47s.png)
用CSS3和HTML5五步打造便签效果
2012-04-25 20:47:51
基于Python实现文件分类器的示例代码
2023-06-02 12:49:10
![](https://img.aspxhome.com/file/2023/9/90279_0s.png)
python数字图像处理环境安装与配置过程示例
2023-03-05 07:00:25
![](https://img.aspxhome.com/file/2023/0/110290_0s.png)
Python+Pygame实战之英文版猜字游戏的实现
2021-01-18 01:40:32
![](https://img.aspxhome.com/file/2023/8/115118_0s.jpg)
如何利用Python模拟GitHub登录详解
2023-11-18 11:08:05
![](https://img.aspxhome.com/file/2023/4/64354_0s.png)
PHP中怎样防止SQL注入分析
2023-08-16 04:45:35
pandas 数据实现行间计算的方法
2021-07-21 06:00:40
Web2.0电子商务网站的交互设计
2009-05-15 12:08:00
![](https://img.aspxhome.com/file/UploadPic/20095/15/01-80s.jpg)
标准的、语义的、Unobtrusive的页签tab切换
2007-11-03 13:58:00
![](https://img.aspxhome.com/file/UploadPic/200711/3/200711314630185s.png)
10个值得关注的优秀CSS框架
2009-05-29 18:11:00
![](https://img.aspxhome.com/file/UploadPic/20095/29/blueprint-27s.png)