亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python實(shí)現(xiàn)加載及解析properties配置文件的方法

 更新時(shí)間:2018年03月29日 12:03:00   作者:tengxing007  
這篇文章主要介紹了Python實(shí)現(xiàn)加載及解析properties配置文件的方法,結(jié)合實(shí)例形式分析了Python針對(duì)properties配置文件的加載、讀取及解析相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)加載及解析properties配置文件的方法。分享給大家供大家參考,具體如下:

這里參考前面一篇:http://chabaoo.cn/article/137393.htm

我們都是在java里面遇到要解析properties文件,在python中基本沒有遇到這中情況,今天用python跑深度學(xué)習(xí)的時(shí)候,發(fā)現(xiàn)有些參數(shù)可以放在一個(gè)global.properties全局文件中,這樣使用的時(shí)候更加方便。原理都是加載文件,然后用line方法進(jìn)行解析判斷”=”,自己從網(wǎng)上找到一個(gè)工具類,記錄一下。

工具類 PropertiesUtiil.py

# -*- coding:utf-8 -*-
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

通過上面的代碼就可以解析了properties文件了。新建一個(gè)文件

global.properties 文件

a.name.last=jie
b.name.first=shi
#b.name=shijie

測(cè)試 test.py

from PropertiesUtil import Properties
dictProperties=Properties("global.properties").getProperties()
print dictProperties

控制臺(tái)打?。?/strong>

/usr/bin/python2.7 /home/tengxing/rude-carnie/test.py
{'a': {'name': {'last': 'jie'}}, 'b': {'name': {'first': 'shi'}}}
Process finished with exit code 0

我感覺還是挺方便的,就對(duì)做深度學(xué)習(xí)來說吧,把模型的的位置,訓(xùn)練數(shù)據(jù)放在一個(gè)global.properties文件中,方便管理。

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論