简体   繁体   English

使用python解析xml数据

[英]using python to parse xml data

I have a question with regards to XML and python. 我对XML和python有疑问。 I want to comb through this xml file and look for certain tags, and then within those tags look for where there is data separated by a comma. 我想梳理这个xml文件并查找某些标签,然后在这些标签内查找用逗号分隔的数据。 split that and make a new line. 拆分并换行。 I have the logic down, im just not too familiar with python to know whoch modules I should be researching. 我有逻辑,我只是​​不太熟悉python以至于不知道我应该研究的whoch模块。 Any help as to where i should start researching would help. 关于我应该从哪里开始研究的任何帮助都会有所帮助。

172.28.18.142,10.0.0.2 172.28.18.142,10.0.0.2

thanks 谢谢

I think when it comes to xml parsing in python there are a few options: lxml , xml , and BeautifulSoup . 我认为在python中进行xml解析时,有几种选择: lxmlxmlBeautifulSoup Most of my experience has dealt with the first two and I've found lxml to be extraordinarily faster than xml . 我的大部分经验都涉及到前两个,并且我发现lxmlxml快得多。 Here's an lxml code snippet for parsing all elements of the root with a particular tag and storing the comma-separated text of each tag as a list. 这是一个lxml代码片段,用于解析带有特定标签的根目录的所有元素,并将每个标签的逗号分隔文本存储为列表。 I think you'll want to add a lot of try and except blocks and tinker with the details, but this should get you started. 我认为您将需要增加很多tryexcept块和修改细节外,但这应该可以帮助您入门。

from lxml import etree

file_path = r'C:\Desktop\some_file.xml'
tree = etree.parse(file_path)
info_list = []
my_tag_path = tree.xpath('//topTag')
for elem in my_tag_path:
    if elem.find('.//childTag') is not None:
        info_list.append(elem.xpath('.//childTag')[0].text.split(','))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM