gpt4 book ai didi

python - 在python中解析结构化文本文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:20:37 24 4
gpt4 key购买 nike

我需要用 Python 解析类似于下面的文本文件,构建数据的分层对象结构,然后对其进行处理。这与我们可以使用 xml.etree.ElementTree 和其他 XML 解析器所做的非常相似。

然而,这些文件的语法不是 XML,我想知道实现这种解析器的最佳方法是什么:如果尝试子类化一个 XML 解析器(哪个?)并自定义其用于标记识别的行为,请编写一个自定义解析器等

{NETLIST topblock
{VERSION 2 0 0}

{CELL topblock
{PORT gearshift_h vpsf vphreg pwron_h vinp vref_out vcntrl_out gd meas_vref
vb vout meas_vcntrl reset_h vinm }
{INST XI21/Mdummy1=pch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/pch_18_mac" Length=0.152 NFIN=8 }
{PIN vpsf=SRC gs_h=DRN vpsf=GATE vpsf=BULK }}
{INST XI21/Mdummy2=nch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/nch_18_mac" Length=0.152 NFIN=5 }
{PIN gs_h=SRC gd=DRN gd=GATE gd=BULK }}
{INST XI20/Mdummy1=pch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/pch_18_mac" Length=0.152 NFIN=8 }
{PIN vpsf=SRC gs_hn=DRN vpsf=GATE vpsf=BULK }}
{INST XI20/Mdummy2=nch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/nch_18_mac" Length=0.152 NFIN=5 }
{PIN gs_hn=SRC gd=DRN gd=GATE gd=BULK }}
{INST XI19/Mdummy1=pch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/pch_18_mac" Length=0.152 NFIN=8 }
{PIN vpsf=SRC net514=DRN vpsf=GATE vpsf=BULK }}
{INST XI19/Mdummy2=nch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/nch_18_mac" Length=0.152 NFIN=5 }
{PIN net514=SRC gd=DRN gd=GATE gd=BULK }}
{INST XI21/MN0=nch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/nch_18_mac" Length=0.152 NFIN=5 }
{PIN gd=SRC gs_h=DRN gs_hn=GATE gd=BULK }}
{INST XI21/MP0=pch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/pch_18_mac" Length=0.152 NFIN=8 }
{PIN vpsf=SRC gs_h=DRN gs_hn=GATE vpsf=BULK }}
{INST XI20/MN0=nch_18_mac {TYPE MOS} {PROP n="sctg_inv1x/nch_18_mac" Length=0.152 NFIN=5 }
...
}
}

最佳答案

其他人在评论中所说的:使用现有的解析器。如果不存在,请自己滚动,但使用解析器库。这里例如与 Parcon :

from pprint import pprint
from parcon import (Forward, SignificantLiteral, Word, alphanum_chars, Exact,
ZeroOrMore, CharNotIn, concat, OneOrMore)

block = Forward()
hyphen = SignificantLiteral('"')
word = Word(alphanum_chars + '/_.)')
value = word | Exact(hyphen + ZeroOrMore(CharNotIn('"')) + hyphen)[concat]
pair = word + '=' + value
flag = word
attribute = pair | flag | block
head = word
body = ZeroOrMore(attribute)
block << '{' + head + body + '}'
blocks = OneOrMore(block)

with open('<your file name>.txt') as infile:
pprint(blocks.parse_string(infile.read()))

结果:

[('NETLIST',
['topblock',
('VERSION', ['2', '0', '0']),
('CELL',
['topblock',
('PORT',
['gearshift_h',
'vpsf',
'vphreg',
'pwron_h',
'vinp',
'vref_out',
'vcntrl_out',
'gd',
'meas_vref',
'vb',
'vout',
'meas_vcntrl',
'reset_h',
'vinm']),
('INST',
[('XI21/Mdummy1', 'pch_18_mac'),
('TYPE', ['MOS']),
('PROP',
[('n', '"sctg_inv1x/pch_18_mac"'),
('Length', '0.152'),
('NFIN', '8')]),
('PIN',
[('vpsf', 'SRC'),
('gs_h', 'DRN'),
('vpsf', 'GATE'),
('vpsf', 'BULK')])]),
('INST',
...

关于python - 在python中解析结构化文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com