Python實現讀取Properties配置文件的方法
更新時間:2018年03月29日 11:40:50 作者:Robin_賓賓
這篇文章主要介紹了Python實現讀取Properties配置文件的方法,結合實例形式分析了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中Django與Echarts的結合用法圖文詳解
ECharts是一個第三方控件,下面這篇文章主要給大家介紹了關于Python中Django與Echarts的結合用法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-10-10python 實現的發(fā)送郵件模板【普通郵件、帶附件、帶圖片郵件】
這篇文章主要介紹了python 實現的發(fā)送郵件模板,包含Python發(fā)送普通郵件、帶附件及帶圖片郵件相關實現技巧,需要的朋友可以參考下2019-07-07