2015-01-07 16 views
0

對於項目,我必須提取在IFC文件中定義的顏色數據。 IFC定義了一個基於EXPRESS的實體關係模型,由幾百個組織成基於對象的繼承層次的實體組成。 IFC文件的將正則表達式寫入文件Python

節爲例:

#3510= IFCCLOSEDSHELL((#3392,#3410,#3421,#3440,#3451,#3462,#3473,#3484,#3495,#3506)); 
#3514= IFCFACETEDBREP(#3510); 
#3517= IFCCOLOURRGB($,0.9372549,0.79215686,0.44705882) 

現在我要實現恢復使用正則表達式在Python中的所有顏色數據。 到目前爲止,我想出了這個(我是新來的編程)

打開IFC文件

IfcFile = open('ifc2.ifc', 'r') 

#defines the string 
IfcColourData = re.compile('ifccolourrgb', re.IGNORECASE) 


#iterating over the ifc file 
for RadColourData in IfcFile: 
    if re.search(IfcColourData, RadColourData): 
     print(RadColourData) 
IfcFile.close()  

#writing the data to a file 
f = open('IFC2RAD.txt', 'w') 
f.write(RadColourData) 
f.close() 

代碼的工作,它會返回在他們IfcColourRGB的ifcfile所有行。 (我可以在我的控制檯中看到)。我在Pydev和Python 3.4中使用Eclipse。

僅當我想將RadColourData的結果寫入名爲IFC2RAD.txt的文件時,它纔會將ifc文件的最後一行寫入IFC2RAD.txt文件。我究竟做錯了什麼?

回答

0

pritinting寫入到文件,這樣以後:

IfcFile = open('ifc2.ifc', 'r') 

#defines the string 
IfcColourData = re.compile('ifccolourrgb', re.IGNORECASE) 

f = open('IFC2RAD.txt', 'w') # opne file to write here 

for RadColourData in IfcFile: 
    if re.search(IfcColourData, RadColourData): 
     print(RadColourData) 
     f.write(RadColourData)  # write here to file 
IfcFile.close()  
f.close()