在 Python 中解析 XML 的最快方法 [英] Fastest way to parse XML in Python

查看:36
本文介绍了在 Python 中解析 XML 的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到从智能手机解析传感器数据以用于实时应用程序的最快捷方式.格式如下:

I'm trying to find the qickest way to parse sensor data from a smartphone for a realtime application. The Format looks like this:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<NodeId>0</NodeId>
<Accelerometer>
    <Accelerometer1>-.1875240802764893</Accelerometer1>
    <Accelerometer2>4.6734819412231445</Accelerometer2>
    <Accelerometer3>8.312667846679688</Accelerometer3>
</Accelerometer>
<Gyroscope>
    <Gyroscope1>-0.10551923513412476</Gyroscope1>
    <Gyroscope2>0.009592439979314804</Gyroscope2>
    <Gyroscope3>0.019185146316885948</Gyroscope3>
</Gyroscope>
<Gravity>
    <Gravity1>-1.2976515293121338</Gravity1>
    <Gravity2>3.672762393951416</Gravity2>
    <Gravity3>9.003327369689941</Gravity3>
</Gravity>
<TimeStamp>1377767599250</TimeStamp>

可用的传感器数据可能会因手机而异.但是一旦建立连接,包的结构就不会改变,所以可能会跳过部分解析.

The available sensor data might change depending on the phone. But once the connection is established the structure of the packages won't change, so maybe parts of the parsing could be skipped.

推荐答案

如果解析速度是您的关键因素,请考虑使用 cElementTreelxml.

If parsing speed is a key factor for you, consider using cElementTree or lxml.

肯定有更多选择,请参阅以下主题:

There are definitely more options out there, see these threads:

这篇关于在 Python 中解析 XML 的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆