使用 python 将 XML 解析为文本文件
Parsing XML to text file using python
我在 XML 中有一个源代码部分,我试图从中以这种方式将值提取到文本文件中。
source1, ipset-1, IPSet, true
source2, ipset-2, IPSet, true
XML 栏目:
<sources excluded="false">
<source>
<name>source1</name>
<value>ipset-1</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
<source>
<name>source2</name>
<value>ipset-2</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
</sources>
目前,我的代码在一行中给出了所有内容。
import xml.etree.ElementTree as ET
tree = ET.fromstring(xml_file)
for node in tree.iter('source'):
print('\n')
with open("source.txt", "a") as file:
for elem in node.iter():
if not elem.tag==node.tag:
file.write("{},".format(elem.text))
print("{}: {}".format(elem.tag, elem.text))
使用beautifulsoup
的解决方案:
from bs4 import BeautifulSoup
xml_doc = """
<sources excluded="false">
<source>
<name>source1</name>
<value>ipset-1</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
<source>
<name>source2</name>
<value>ipset-2</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
</sources>
"""
soup = BeautifulSoup(xml_doc, "lxml")
with open("source.txt", "w") as f_out:
for tag in soup.select("source"):
print(",".join(t.text for t in tag.select("*")), file=f_out)
创建 source.txt
:
source1,ipset-1,IPSet,true
source2,ipset-2,IPSet,true
我在 XML 中有一个源代码部分,我试图从中以这种方式将值提取到文本文件中。
source1, ipset-1, IPSet, true
source2, ipset-2, IPSet, true
XML 栏目:
<sources excluded="false">
<source>
<name>source1</name>
<value>ipset-1</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
<source>
<name>source2</name>
<value>ipset-2</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
</sources>
目前,我的代码在一行中给出了所有内容。
import xml.etree.ElementTree as ET
tree = ET.fromstring(xml_file)
for node in tree.iter('source'):
print('\n')
with open("source.txt", "a") as file:
for elem in node.iter():
if not elem.tag==node.tag:
file.write("{},".format(elem.text))
print("{}: {}".format(elem.tag, elem.text))
使用beautifulsoup
的解决方案:
from bs4 import BeautifulSoup
xml_doc = """
<sources excluded="false">
<source>
<name>source1</name>
<value>ipset-1</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
<source>
<name>source2</name>
<value>ipset-2</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
</sources>
"""
soup = BeautifulSoup(xml_doc, "lxml")
with open("source.txt", "w") as f_out:
for tag in soup.select("source"):
print(",".join(t.text for t in tag.select("*")), file=f_out)
创建 source.txt
:
source1,ipset-1,IPSet,true
source2,ipset-2,IPSet,true