利用python读取xml中的数据

目录

xml例子

方法一:利用cElementTree

方法二:利用read_xml()

方法三:利用pd.json_normalize()


xml例子

xml = '''<?xml version='1.0' encoding='utf-8'?>
<data>
 <row>
   <shape>square</shape>
   <degrees>360</degrees>
   <sides>4.0</sides>
 </row>
 <row>
   <shape>circle</shape>
   <degrees>360</degrees>
 </row>
 <row>
   <shape>triangle</shape>
   <degrees>180</degrees>
   <sides>3.0</sides>
 </row>
</data>'''

方法一:利用cElementTree

from xml.etree import cElementTree as ET
import pandas as pd
​
# 读取xml字符串
root = ET.fromstring(text=xml)
# 读取xml文件
# tree = ET.ElementTree(file="text.xml")  
# root = tree.getroot() 
data = list()
for child in root:
    data1 = list()
    for son in child:
        data1.append(son.text)
    data.append(data1)
​
df = pd.DataFrame(data, columns=['shape', 'degrees', 'sides'])
print(df)
输出结果:
    shape  degrees  sides
0    square      360    4.0
1    circle      360    NaN
2  triangle      180    3.0

如果 shape 、degrees、sides 不是按照一定规律排列,这样取数据容易出错。

比如将最后一组 degrees、 shape 、sides ,

输出结果便会变成:

    shape   degrees sides
0  square       360   4.0
1  circle       360  None
2     180  triangle   3.0

方法二:利用read_xml()

import pandas as pd
df = pd.read_xml(xml)
print(df)
输出结果:
    shape  degrees  sides
0    square      360    4.0
1    circle      360    NaN
2  triangle      180    3.0

方法三:利用pd.json_normalize()

  • 将xml转为类似json的格式

  • 利用pd.json_normalize() 读到dataframe

def fun1(root):
    dic1 = dict()
    for child in root:
        if bool(child) is True:  # 有下一层
            print(child.tag)
            dic2 = fun1(child)  # 自己调用自己
            value = dic1.get(child.tag)  # 存在返回,不存在返回None
            if value:  # 存在
                value.append(dic2)
                dic1[child.tag] = value
            else:
                dic1[child.tag] = [dic2]
        else:
            dic1[child.tag] = child.text
    return dic1

if __name__ == '__main__':
    from xml.etree import cElementTree as ET
    import pandas as pd

    root = ET.fromstring(text=xml)

    dic1 = fun1(root)
    df = pd.json_normalize(dic1['row'])
    print(df)
输出结果:
    shape  degrees  sides
0    square      360    4.0
1    circle      360    NaN
2  triangle      180    3.0
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值