Python从单个标记解析XML变量

Python从单个标记解析XML变量,python,xml,parsing,python-2.7,xml-parsing,Python,Xml,Parsing,Python 2.7,Xml Parsing,我有一个XML文件,看起来像下面的代码: <spotter num="0187" report_at="2014-03-15 20:10:25" lat="49.8696518" lng="-80.0973129"callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last=""></spotter> 我尝试过使用dom.minidom,但是如

我有一个XML文件,看起来像下面的代码:

<spotter num="0187" report_at="2014-03-15 20:10:25" lat="49.8696518" lng="-80.0973129"callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last=""></spotter>

我尝试过使用dom.minidom,但是如何从XML文件中轻松解析lat和lng变量值呢


提前谢谢你的帮助

您需要使用XML解析器,例如,或

下面是一个使用标准库中的
ElementTree
的示例:

from xml.etree import ElementTree as ET

tree = ET.fromstring("""
<test>
    <spotter num="0187" report_at="2014-03-15 20:10:25" lat="49.8696518" lng="-80.0973129" callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last=""/>
</test>""")
spotter = tree.find('.//spotter')
print spotter.attrib['lat'], spotter.attrib['lng']
两种印刷品:

49.8696518 -80.0973129
就格式良好的xml结构而言,
beautifulsoub
更为宽容(请参阅,我必须对xml进行一些编辑,以使
ElementTree
工作正常),而且它实际上更易于使用


希望能有所帮助。

Pyparsing有一个内置的方法,可以从HTML标记中提取属性,而不必为整个页面构建完整的对象模型

html = """
<spotter num="0187" report_at="2014-03-15 20:10:25" lat="49.8696518" lng="-80.0973129" callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last="">

I've tried using dom.minidom, but how can I easily parse out the lat and lng variable values fro
<spotter num="0188" report_at="2014-03-15 20:11:25" lat="59.8696518" lng="-82.0973129" callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last="">

"""

from pyparsing import makeHTMLTags

spotterTag, spotterEndTag = makeHTMLTags("spotter")

for spotter in spotterTag.searchString(html):
    print spotter.report_at
    print spotter.num
    print spotter.lat
    print spotter.lng
    print spotter.email
    print
html = """
<spotter num="0187" report_at="2014-03-15 20:10:25" lat="49.8696518" lng="-80.0973129" callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last="">

I've tried using dom.minidom, but how can I easily parse out the lat and lng variable values fro
<spotter num="0188" report_at="2014-03-15 20:11:25" lat="59.8696518" lng="-82.0973129" callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last="">

"""

from pyparsing import makeHTMLTags

spotterTag, spotterEndTag = makeHTMLTags("spotter")

for spotter in spotterTag.searchString(html):
    print spotter.report_at
    print spotter.num
    print spotter.lat
    print spotter.lng
    print spotter.email
    print
2014-03-15 20:10:25
0187
49.8696518
-80.0973129
addu@nnu.nne

2014-03-15 20:11:25
0188
59.8696518
-82.0973129
addu@nnu.nne