如何从嵌套的XML创建Pandas DataFrame
在这篇文章中,我们将学习如何从嵌套的XML中创建Pandas DataFrame。我们将使用xml.etree.ElementTree模块,它是Python中的一个内置模块,用于解析或读取XML文件中的信息。ElementTree将XML文件表示为一棵树,Element只代表树的一个节点。
使用的方法:
在这里,我们将使用一些函数来处理下面所说的代码。
- ElementTree.parse( XML_file) 。从一个XML文件中读取数据
- root.iter(‘root_name’)。遍历根节点的各个分支
- ElementTree.fromstring(XML_file) 。读取XML代码中的数据,该代码在Python代码中以三引号内的字符串形式传递。
- prstree.findall(‘store’)。要找到解析过的XML ElementTree的所有元素
- node.attribute.get(attribte_name)。为了获得属性
- node.find(attribte_name)。检索所提到的attribute_name的文本内容
- pandas.DataFrame() 。将XML数据转换为一个DataFrame
- list.append()。将项目追加到一个列表中
步骤
- 使用ElementTree.parse( )函数解析或读取XML文件,获得根元素。
- 遍历根节点以获得子节点属性’SL NO’(这里),并提取每个属性的文本值(这里是foodItem、价格、数量和折扣)。
- 获取各自的食品项目,并将其规格作为一个单位附加到一个列表(这里是all_items()列表)。
- 使用pandas.DataFrame()函数将列表转换为DataFrame,并在引号内提及由逗号分隔的列名。
- 打印DataFrame,就完成了。
输入嵌套的XML数据
<?xml version="1.0" encoding="UTF-8"?>
<Food>
<Info>
<Msg>Food Store items.</Msg>
</Info>
<store slNo="1">
<foodItem>meat</foodItem>
<price>200</price>
<quantity>1kg</quantity>
<discount>7%</discount>
</store>
<store slNo="2">
<foodItem>fish</foodItem>
<price>150</price>
<quantity>1kg</quantity>
<discount>5%</discount>
</store>
<store slNo="3">
<foodItem>egg</foodItem>
<price>100</price>
<quantity>50 pieces</quantity>
<discount>5%</discount>
</store>
<store slNo="4">
<foodItem>milk</foodItem>
<price>50</price>
<quantity>1 litre</quantity>
<discount>3%</discount>
</store>
</Food>
示例 1:
在下面的代码中,我们已经解析了XML文件。在引号内给出你保存XML文件的完整路径。因此,在这里我们需要使用ElementTree.parse()函数从XML文件中读取数据,然后使用getroot()函数来获取根。然后按照给出的步骤操作。
import xml.etree.ElementTree as ETree
import pandas as pd
# give the path where you saved the xml file
# inside the quotes
xmldata = "C: \\ProgramData\\Microsoft\\
Windows\\Start Menu\\Programs\\
Anaconda3(64-bit)\\xmltopandas.xml"
prstree = ETree.parse(xmldata)
root = prstree.getroot()
# print(root)
store_items = []
all_items = []
for storeno in root.iter('store'):
store_Nr = storeno.attrib.get('slNo')
itemsF = storeno.find('foodItem').text
price = storeno.find('price').text
quan = storeno.find('quantity').text
dis = storeno.find('discount').text
store_items = [store_Nr, itemsF, price, quan, dis]
all_items.append(store_items)
xmlToDf = pd.DataFrame(all_items, columns=[
'SL No', 'ITEM_NUMBER', 'PRICE', 'QUANTITY', 'DISCOUNT'])
print(xmlToDf.to_string(index=False))
输出:
注意 : XML文件应该保存在你的Python代码所保存的同一个目录或文件夹中。
示例 2:
我们也可以把XML内容作为三引号内的一个字符串来传递。在这种情况下,我们需要使用fromstring()函数来读取该字符串。使用’tag’对象获得根,并按照上面提到的相同步骤将其转换为DataFrame。
import xml.etree.ElementTree as ETree
import pandas as pd
xmldata = '''<?xml version="1.0" encoding="UTF-8"?>
<Food>
<Info>
<Msg>Food Store items.</Msg>
</Info>
<store slNo="1">
<foodItem>meat</foodItem>
<price>200</price>
<quantity>1kg</quantity>
<discount>7%</discount>
</store>
<store slNo="2">
<foodItem>fish</foodItem>
<price>150</price>
<quantity>1kg</quantity>
<discount>5%</discount>
</store>
<store slNo="3">
<foodItem>egg</foodItem>
<price>100</price>
<quantity>50 pieces</quantity>
<discount>5%</discount>
</store>
<store slNo="4">
<foodItem>milk</foodItem>
<price>50</price>
<quantity>1 litre</quantity>
<discount>3%</discount>
</store>
</Food>
'''
prstree = ETree.fromstring(xmldata)
root = prstree.tag
#print(root)
store_items = []
all_items = []
for storeno in prstree.findall('store'):
store_Nr = storeno.attrib.get('slNo')
itemsF= storeno.find('foodItem').text
price= storeno.find('price').text
quan= storeno.find('quantity').text
dis= storeno.find('discount').text
store_items = [store_Nr,itemsF,price,quan,dis]
all_items.append(store_items)
xmlToDf = pd.DataFrame(all_items,columns=[
'SL No','ITEM_NUMBER','PRICE','QUANTITY','DISCOUNT'])
print(xmlToDf.to_string(index=False))
输出: