溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Python怎么對(duì)properties配置文件進(jìn)行讀取

發(fā)布時(shí)間:2021-03-05 17:22:29 來源:億速云 閱讀:559 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)使用Python怎么對(duì)properties配置文件進(jìn)行讀取,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Python主要用來做什么

Python主要應(yīng)用于:1、Web開發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲;4、嵌入式應(yīng)用開發(fā);5、游戲開發(fā);6、桌面應(yīng)用開發(fā)。

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

測(cè)試文件text.py:

from Util import Properties
dictProperties=Properties("filename.properties").getProperties()
print dictProperties

輸出:

{'a': {'c': 'v2', 'b': {'d': 'v1'}}, 'd': {'e': 'v3'}, 'f': 'v4'}

看完上述內(nèi)容,你們對(duì)使用Python怎么對(duì)properties配置文件進(jìn)行讀取有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI