[英]Parsing different data types with Python
我想解析以下行:
#3 = IFCPERSONANDORGANIZATION(#4,#5,$);
并想提取数字:3、4、5为整数值,并将“ IFCPERSONANDORGANIZATION”作为字符串值,以便将这些属性保存在具有networkx的图形中。
这是我的代码:
data = []
with open('test.ifc') as f:
for line in f:
if line.startswith('#'):
words = line.rstrip().split('#')
print(words)
node = int(words[0])
data.append(node)
错误:ValueError:int()以10为底的无效文字:
如果每次的行结构都不相同,如何使用正则表达式? 像这样:
#3 = IFCPERSONANDORGANIZATION(#4, #5, $);
#2 = IFCOWNERHISTORY(#3, #6, $, .NOTDEFINED., $, $, $, 1348486883);
#4 = IFCPERSON($, 'Bonsma', 'Peter', $, $, $, $, $);
#5 = IFCORGANIZATION($, 'RDF', 'RDF Ltd.', $, $);
#6 = IFCAPPLICATION(#5, '0.10', 'Test Application', 'TA 1001');****
您可以使用正则表达式:
import re
line = '#3 = IFCPERSONANDORGANIZATION(#4, #5, $);'
node, name, a, b = re.search(r'(\d+) = (\w+)\(#(\d+), #(\d+), \$\)', line).groups()
node, a, b = map(int, [node, a, b])
print(node, name, a, b)
版画
3 IFCPERSONANDORGANIZATION 4 5
可能是较晚的评论,但是当我进行类似搜索时,我想出了您的问题并给出了答案。 @ user3926906 IFC文件结构通常会针对每个不同的文件而更改。 当您使用re.search()
你体验到分裂的任何挑战#
实体? 我问是因为某些实体没有#
引用其他实体。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.