将正则表达式写入文件 Python
Writing Regular Expressions to a file Python
对于一个项目,我必须提取 IFC 文件中定义的颜色数据。
IFC 定义了一个基于 EXPRESS 的实体关系模型,该模型由组织成基于对象的继承层次结构的数百个实体组成。
例如 IFC 文件的部分:
#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()
代码有效,它 returns ifc 文件中的所有行都包含 IfcColourRGB。 (我在控制台中可以看到的内容)。我在 Pydev 和 Python 3.4.
中使用 Eclipse
仅当我想将 RadColourData 的结果写入名为 IFC2RAD.txt 的文件时,它只会将 ifc 文件的最后一行写入 IFC2RAD.txt 文件。我做错了什么?
打印后写入文件,像这样:
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()
对于一个项目,我必须提取 IFC 文件中定义的颜色数据。 IFC 定义了一个基于 EXPRESS 的实体关系模型,该模型由组织成基于对象的继承层次结构的数百个实体组成。
例如 IFC 文件的部分:
#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()
代码有效,它 returns ifc 文件中的所有行都包含 IfcColourRGB。 (我在控制台中可以看到的内容)。我在 Pydev 和 Python 3.4.
中使用 Eclipse仅当我想将 RadColourData 的结果写入名为 IFC2RAD.txt 的文件时,它只会将 ifc 文件的最后一行写入 IFC2RAD.txt 文件。我做错了什么?
打印后写入文件,像这样:
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()