2011-03-18 110 views
0

我之前發佈了這個問題Approach for parsing file and creating dynamic data structure for use by another program,並得到了一些很好的建議。重新模塊腳本的幫助

所以,現在我有一些工作,我想看看是否有更好的方式來做到這一點。

背景資料:

我基本上需要解析的靜態屬性文件,我不能在這個時候修改。從中讀取一些值並將其放入一個數據結構中,我可以在一些我可以控制的腳本中使用它們。我面臨的問題是我想將「like」鍵/值組合在一起,以便稍後在腳本中以循環的方式訪問它們。例如,我的屬性文件實際上看起來像這樣。

somerandom.propname=value1 
myhostname.APP_HOME=c:\\apps\\apphome 
myhostname.DomainName=sampleDomain 
myhostname.data.dir=D:\\data\\ 
sampleDomain.host=myhostname 
sampleDomain.port=80 
sampleDomain.sslport=443 
sampleDomain.ClusterManagedServer1.port=8080 
sampleDomain.ClusterManagedServer2.port=8012 
sampleDomain.ClusterManagedServer1.sslport=8021 
sampleDomain.ClusterManagedServer2.sslport=8022 
sampleDomain.ClusterManagedServer1.name=MS1 
sampleDomain.ClusterManagedServer2.name=MS2 
sampleDomain.ClusterManagedServer1.host=myhostname 
sampleDomain.ClusterManagedServer2.host=myHOST2 
sampleDomain.ClusterManagedServer3.port=8031 
sampleDomain.ClusterManagedServer4.port=8042 
sampleDomain.ClusterManagedServer3.sslport=8043 
sampleDomain.ClusterManagedServer4.sslport=8053 
sampleDomain.ClusterManagedServer3.name=MS3 
sampleDomain.ClusterManagedServer4.name=MS4 
sampleDomain.ClusterManagedServer3.host=myhostHOST3 
sampleDomain.ClusterManagedServer4.host=myhostHOST4 
sampleDomain.AdminAccount=samplesuperadmin 
sampleDomain.ServerName=myhostname222 
sampleDomain.ServerPort=8008 
sampleDomain.HeapSize=1234m 

somerandom.propname=value2 
anotherhostname.APP_HOME=c:\\apps\\apphome 
anotherhostname.DomainName=sampleDomain 
anotherhostname.data.dir=D:\\data\\ 
testDomain.host=anotherhostname 
testDomain.port=80 
testDomain.sslport=443 
testDomain.ClusterManagedServer1.port=8080 
testDomain.ClusterManagedServer2.port=8012 
testDomain.ClusterManagedServer1.sslport=8021 
testDomain.ClusterManagedServer2.sslport=8022 
testDomain.ClusterManagedServer1.name=MS1 
testDomain.ClusterManagedServer2.name=MS2 
testDomain.ClusterManagedServer1.host=anotherhostname 
testDomain.ClusterManagedServer2.host=anotherHOST2 
testDomain.ClusterManagedServer3.port=8031 
testDomain.ClusterManagedServer4.port=8042 
testDomain.ClusterManagedServer3.sslport=8043 
testDomain.ClusterManagedServer4.sslport=8053 
testDomain.ClusterManagedServer3.name=MS3 
testDomain.ClusterManagedServer4.name=MS4 
testDomain.ClusterManagedServer3.host=anotherHOST3 
testDomain.ClusterManagedServer4.host=anotherHOST4 
testDomain.AdminAccount=superadminaccount 
testDomain.ServerName=myservernamehere 
testDomain.ServerPort=80 
testDomain.HeapSize=1355m 

那麼我現在要做的是配合了「ClusterManagedServer#」,其中#=到一些結構中的號碼,以便我能環通,併爲每個主機confgure名,端口的所有值,等等

我有以下代碼工作(只是一個概念證明),我不得不使用現有的模塊,與早期版本的Python,所以我不能下載任何模塊,並安裝它們。

import re 
from socket import gethostname 

class Ddict(dict): 
    def __init__(self, default=None): 
     self.default = default 

    def __getitem__(self, key): 
     if not self.has_key(key): 
      self[key] = self.default() 
     return dict.__getitem__(self, key) 


propertyfile_name = 'SystemConfig.properties' 

hname=gethostname() 
print 'hostname is', hname 

print '\ngetting domainname\n' 
RE4 =(hname+'.DomainName=(\S+)(?:\s+|\Z)') 

with open(propertyfile_name) as f: 
    for (x) in re.findall(RE4,f.read()): 
     dName=x 
     print 'domain name is: ',dName 

RE = (dName+'.ClusterManagedServer(\d+)\.host=(\S+)(?:\s+|\Z)') 
RE1 =(dName+'.ClusterManagedServer(\d+)\.name=(\S+)(?:\s+|\Z)') 
RE2 =(dName+'.ClusterManagedServer(\d+)\.sslport=(\d+)\s+') 
RE3 =(dName+'.ClusterManagedServer(\d+)\.port=(\d+)\s+') 

dico = Ddict(dict) 

print '\n\nGetting values from property file:' 


with open(propertyfile_name) as f: 
    for (server,h) in re.findall(RE,f.read()): 
     dico[server]['host']=h  

with open(propertyfile_name) as f: 
    for (server,n) in re.findall(RE1,f.read()): 
     dico[server]['name']=n  
     #print dico 

with open(propertyfile_name) as f: 
    for (server,s) in re.findall(RE2,f.read()): 
     dico[server]['sslport']=s  
     #print dico 

with open(propertyfile_name) as f: 
    for (server,p) in re.findall(RE3,f.read()): 
     dico[server]['port']=p 
     #print dico 

#print '\ndico is:' 
#print dico 

print '\n**for loop to print out values in dictionary **\n' 
keys = dico.keys() 
keys.sort() 
for k in keys: 
    print '\n' 
    #print k 
    #print dico[k] 
    print dico[k]['name'] 
    print dico[k]['sslport'] 
    print dico[k]['host'] 
    print dico[k]['port'] 
    print '\n' 

我的問題是,是否有更好的方法來解析該文件,並會得到什麼,我需要不必打開文件的4倍,以獲得特定的值,而不是?

感謝

+0

我在編輯它,當別人做了的中間。給我休息一下? – user275633 2011-03-18 20:58:37

+0

@ user275633:在發佈之前,您應該能夠看到代碼。許多許多人沒有形成問題的格式,似乎也不知道他們沒有格式化他們的問題。由於缺乏ESP,我無法分辨您是否不知道格式化。由於我的ESP完全失敗,我問。 – 2011-03-18 21:00:56

+0

是的,你是正確的,你可以像其他許多人一樣,當我被剪切/粘貼時,我忘了縮進它。我發佈後發現的簡單錯誤。猜猜你需要調整那個ESP;)。感謝您的回覆。 – user275633 2011-03-19 02:52:59

回答

0
with open(propertyfile_name) as f: 
    content= f.read() 
    for (server,h) in re.findall(RE,content): 
     dico[server]['host']=h 

    for (server,n) in re.findall(RE1,content): 
     dico[server]['name']=n 

    for (server,s) in re.findall(RE2,content): 
     dico[server]['sslport']=s 

    for (server,p) in re.findall(RE3,content): 
     dico[server]['port']=p 
+0

感謝您的回覆。有沒有更好的選項來簡化正則表達式? – user275633 2011-03-18 21:03:18

+0

很少。我不確定你想象什麼改進可能是可能的。這是一個屬性文件,如果您正在問的是什麼,您可以輕鬆地解析該行而不使用任何**正則表達式。 – 2011-03-18 21:17:29