你考虑过使用xpath吗?它是一种迷你语言,可以用来绕过xml树。在python中可以很容易地使用它。在
下面是我应该怎么做(这是有效的Python代码。我用Python3.2测试过。可以很好地处理示例xml):import xml.etree.ElementTree as xml #you had this line in your code. I am not using any tool you do not have access to in your script
def get_row_attributes(the_xml_as_a_string):
"""
this function takes xml as a string.
It can work with xml that looks like your included example xml.
This function returns a list of dictionaries. Each dictionary is made up of the attributes of each row. So the result looks like:
[
{attribute_name:value_for_first_row,attribute_name:value_for_first_row...},
{attribute_name:value_for_second_row,attribute_name:value_for_second_row...},
etc
]
"""
tree = xml.fromstring(the_xml_as_a_string)
rows = tree.findall('table/row') # 'table/row' is xpath. it means get all the rows in all the tables
return [row.attrib for row in rows]
要使用此函数,请读入std并建立一个字符串。呼叫get_row_attributes(the_xml_as_a_string)
生成的字典包含您请求的信息(行的属性)。在
所以现在我们有了从std读入
得到了所有行的所有信息
全部使用完全正常的python
最后要做的是将它写入另一个进程。如果您需要有关此部分的帮助,请提供有关数据应采用何种格式以及数据应流向何处的信息