0

我有一个类似 xml 的文件,该文件在属性中没有引号,attribute="xxx"并且没有标准<?xml version="1.0"?>标题,因此当我尝试使用minidomor解析时elementtree,他们将文件抱怨为not well-formed

>>> import xml.etree.ElementTree as et
>>> tree = et.parse(infile)
>>> Traceback (most recent call last):
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 25

如何读取输入文件?或者我怎样才能使 xml 格式正确?

我的输入文件如下所示:

<contextfile concordance=brown>
<context filename=br-a01 paras=yes>
<p pnum=1>
<s snum=1>
<wf cmd=ignore pos=DT>The</wf>
<wf cmd=done rdf=group pos=NNP lemma=group wnsn=1 lexsn=1:03:00:: pn=group>Fulton_County_Grand_Jury</wf>
<wf cmd=done pos=VB lemma=say wnsn=1 lexsn=2:32:00::>said</wf>
<wf cmd=done pos=NN lemma=friday wnsn=1 lexsn=1:28:00::>Friday</wf>
<wf cmd=ignore pos=DT>an</wf>
<wf cmd=done pos=NN lemma=investigation wnsn=1 lexsn=1:09:00::>investigation</wf>
<wf cmd=ignore pos=IN>of</wf>
<wf cmd=done pos=NN lemma=atlanta wnsn=1 lexsn=1:15:00::>Atlanta</wf>
<wf cmd=ignore pos=POS>'s</wf>
<wf cmd=done pos=JJ lemma=recent wnsn=2 lexsn=5:00:00:past:00>recent</wf>
<wf cmd=done pos=NN lemma=primary_election wnsn=1 lexsn=1:04:00::>primary_election</wf>
<wf cmd=done pos=VB lemma=produce wnsn=4 lexsn=2:39:01::>produced</wf>
<punc>``</punc>
<wf cmd=ignore pos=DT>no</wf>
<wf cmd=done pos=NN lemma=evidence wnsn=1 lexsn=1:09:00::>evidence</wf>
<punc>''</punc>
<wf cmd=ignore pos=IN>that</wf>
<wf cmd=ignore pos=DT>any</wf>
<wf cmd=done pos=NN lemma=irregularity wnsn=1 lexsn=1:04:00::>irregularities</wf>
<wf cmd=done pos=VB lemma=take_place wnsn=1 lexsn=2:30:00::>took_place</wf>
<punc>.</punc>
</s>
</p>
</context>
</contextfile>
4

1 回答 1

1

使用lxml

mytext="""<contextfile concordance=brown>
<context filename=br-a01 paras=yes>
<p pnum=1>
....
<wf cmd=done pos=VB lemma=say wnsn=1 lexsn=2:32:00::>said</wf>
<wf cmd=done pos=NN lemma=friday wnsn=1 lexsn=1:28:00::>Friday</wf>
<wf cmd=ignore pos=DT>an</wf>
....
....
<punc>``</punc>
<wf cmd=ignore pos=DT>no</wf>
<wf cmd=done pos=NN lemma=evidence wnsn=1 lexsn=1:09:00::>evidence</wf>
<punc>''</punc>
....
<wf cmd=done pos=NN lemma=irregularity wnsn=1 lexsn=1:04:00::>irregularities</wf>
<punc>.</punc>
</s>
</p>
</context>
</contextfile>"""

from lxml import html
parsed = html.fromstring(mytext)
for x in parsed.getiterator(): print x.tag, x.attrib, x.text, x.tail

输出:

contextfile {'concordance': 'brown'} None None
context {'paras': 'yes', 'filename': 'br-a01'} None None
p {'pnum': '1'} 
....


wf {'lemma': 'say', 'cmd': 'done', 'wnsn': '1', 'pos': 'VB', 'lexsn': '2:32:00::'} said None
wf {'lemma': 'friday', 'cmd': 'done', 'wnsn': '1', 'pos': 'NN', 'lexsn': '1:28:00::'} Friday None
wf {'cmd': 'ignore', 'pos': 'DT'} an 
....
....

punc {} `` None
wf {'cmd': 'ignore', 'pos': 'DT'} no None
wf {'lemma': 'evidence', 'cmd': 'done', 'wnsn': '1', 'pos': 'NN', 'lexsn': '1:09:00::'} evidence None
punc {} '' 
....

wf {'lemma': 'irregularity', 'cmd': 'done', 'wnsn': '1', 'pos': 'NN', 'lexsn': '1:04:00::'} irregularities None
punc {} . None
于 2013-08-20T10:01:43.227 回答