在Python中从tcx文件读取和打印值

时间:2019-05-25 17:57:35

标签: python xml dom

我正在编写Python代码以读取从我的Polar心率监视器下载的TCX文件。我尝试使用xml.dom库,但得到的似乎是某种类或内存位置。

我正在查看的数据在锻炼文件中具有每秒的心率(和其他信息)。

这是数据文件的一部分:

<Trackpoint>
    <Time>2019-05-24T12:12:10.000Z</Time>
    <Position><LatitudeDegrees>37.09307833</LatitudeDegrees
    <LongitudeDegrees>-84.60697</LongitudeDegrees></Position>
    <DistanceMeters>0.10000000149011612</DistanceMeters>
    <HeartRateBpm><Value>73</Value></HeartRateBpm>
    <Cadence>0</Cadence><SensorState>Present</SensorState>
</Trackpoint>
<Trackpoint>
    <Time>2019-05-24T12:12:11.000Z</Time>
    <Position><LatitudeDegrees>37.09307667</LatitudeDegrees>
    <LongitudeDegrees>-84.60697667</LongitudeDegrees></Position>
    <HeartRateBpm><Value>73</Value></HeartRateBpm>
    <Cadence>0</Cadence><SensorState>Present</SensorState>
</Trackpoint>
<Trackpoint>
    <Time>2019-05-24T12:12:12.000Z</Time>
    <Position><LatitudeDegrees>37.09307167</LatitudeDegrees>
    <LongitudeDegrees>-84.60698167</LongitudeDegrees></Position>
    <DistanceMeters>0.800000011920929</DistanceMeters>
    <HeartRateBpm><Value>73</Value></HeartRateBpm>
    <Cadence>0</Cadence><SensorState>Present</SensorState>
</Trackpoint>

这是我正在使用的代码:

from xml.dom import minidom
from datetime import *

xmldoc = minidom.parse("testfile.tcx")
print(xmldoc)


tcd = xmldoc.getElementsByTagName("TrainingCenterDatabase")[0]

activitiesElement = tcd.getElementsByTagName("Activities")[0]

activities = activitiesElement.getElementsByTagName("Activity")

for activity in activities:
    sport = activity.attributes["Sport"]
    sportName = sport.value

    idElement = activity.getElementsByTagName("Id")[0]
    timeOfDay = idElement.firstChild.data
    year = int(timeOfDay[0:4])
    month = int(timeOfDay[5:7])
    day = int(timeOfDay[8:10])
    date = datetime(year,month,day)
    #print(sportName, month, day, year)
    print(sportName, date)


trackPoints = tcd.getElementsByTagName("Time")
heartRate = tcd.getElementsByTagName("Value")


print(type(trackPoints))
print(type(heartRate))

i=0

while i <= 10:
    print(trackPoints[i], heartRate[i])
    i += 1

这是我从该程序获得的输出:

C:\Python\Heart Rate Data>python "c:/Python/Heart Rate 
Data/readfiletest4.py"
<xml.dom.minidom.Document object at 0x00000217F5A86948>
Running 2019-05-24 00:00:00
<class 'xml.dom.minicompat.NodeList'>
<class 'xml.dom.minicompat.NodeList'>
<DOM Element: Time at 0x217f5a92048> <DOM Element: Value at 0x217f5a4daf8>
<DOM Element: Time at 0x217f5a92638> <DOM Element: Value at 0x217f5a4dc28>
<DOM Element: Time at 0x217f5a92b90> <DOM Element: Value at 0x217f5a923d8>
<DOM Element: Time at 0x217f5aae210> <DOM Element: Value at 0x217f5a92930>
<DOM Element: Time at 0x217f5aae768> <DOM Element: Value at 0x217f5a92f20>
<DOM Element: Time at 0x217f5aaed58> <DOM Element: Value at 0x217f5aae508>
<DOM Element: Time at 0x217f5aaf3d8> <DOM Element: Value at 0x217f5aaeaf8>
<DOM Element: Time at 0x217f5aaf9c8> <DOM Element: Value at 0x217f5aaf178>
<DOM Element: Time at 0x217f5ab0048> <DOM Element: Value at 0x217f5aaf768>
<DOM Element: Time at 0x217f5ab0638> <DOM Element: Value at 0x217f5aafd58>
<DOM Element: Time at 0x217f5ab0c28> <DOM Element: Value at 0x217f5ab03d8>

我希望从TCX文件中获得具有各种元素的数组,以便最终实现一些图形绘制等。

1 个答案:

答案 0 :(得分:0)

为了获取XML元素的文本,您需要使用元素的'text'属性。

(您可以查看此代码https://github.com/vkurup/python-tcxparser

while i <= 10:
    print(trackPoints[i].text, heartRate[i].text)
    i += 1

下面是一个完整的工作示例(基于示例XML):

import xml.etree.ElementTree as ET
import re

with open('1.tcx') as xml_file:
    xml_str = xml_file.read()
    xml_str = re.sub(' xmlns="[^"]+"', '', xml_str.decode('utf-8'), count=1)
    root = ET.fromstring(xml_str)
    activities = root.findall('.//Activity')
    for activity in activities:
        print('-- {} --'.format(activity.attrib['Sport']))
        tracking_points = activity.findall('.//Trackpoint')
        for tracking_point in list(tracking_points):
            children = list(tracking_point)
            print('Time: {}, HR Value: {}'.format(children[0].text,
                                                  list(children[4])[0].text))

1.tcx

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation
="http://www.garmin.com/xmlschemas/ActivityExtension/v2 http://www.garmin.com/xmlschemas/ActivityExtensionv2.xsd http://www.garmin.com/xmlschemas/TrainingCenterDat
abase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">

  <Activities>
    <Activity Sport="Biking">
      <Id>2010-06-26T10:06:11Z</Id>
      <Lap StartTime="2010-06-26T10:06:11Z">
        <TotalTimeSeconds>906.1800000</TotalTimeSeconds>
        <DistanceMeters>9762.4433594</DistanceMeters>
        <MaximumSpeed>15.2404995</MaximumSpeed>
        <Calories>493</Calories>
        <AverageHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
          <Value>179</Value>
        </AverageHeartRateBpm>
        <MaximumHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
          <Value>194</Value>
        </MaximumHeartRateBpm>
        <Intensity>Active</Intensity>
        <Cadence>84</Cadence>
        <TriggerMethod>Location</TriggerMethod>
        <Track>
          <Trackpoint>
            <Time>2010-06-26T10:06:11Z</Time>
            <Position>
              <LatitudeDegrees>40.7780135</LatitudeDegrees>
              <LongitudeDegrees>-73.9665795</LongitudeDegrees>
            </Position>
            <AltitudeMeters>36.1867676</AltitudeMeters>
            <DistanceMeters>0.0629519</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>148</Value>
            </HeartRateBpm>
            <SensorState>Absent</SensorState>
          </Trackpoint>
          <Trackpoint>
            <Time>2010-06-26T10:06:12Z</Time>
            <Position>
              <LatitudeDegrees>40.7780136</LatitudeDegrees>
              <LongitudeDegrees>-73.9665778</LongitudeDegrees>
            </Position>
            <AltitudeMeters>35.2254639</AltitudeMeters>
            <DistanceMeters>0.1698548</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>148</Value>
            </HeartRateBpm>
            <SensorState>Absent</SensorState>
          </Trackpoint>
          <Trackpoint>
            <Time>2010-06-26T10:06:17Z</Time>
            <Position>
              <LatitudeDegrees>40.7780147</LatitudeDegrees>
              <LongitudeDegrees>-73.9665789</LongitudeDegrees>
            </Position>
            <AltitudeMeters>34.2641602</AltitudeMeters>
            <DistanceMeters>0.5907414</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>152</Value>
            </HeartRateBpm>
            <SensorState>Absent</SensorState>
          </Trackpoint>
          <Trackpoint>
            <Time>2010-06-26T10:06:24Z</Time>
            <Position>
              <LatitudeDegrees>40.7780246</LatitudeDegrees>
              <LongitudeDegrees>-73.9665919</LongitudeDegrees>
            </Position>
            <AltitudeMeters>34.7448730</AltitudeMeters>
            <DistanceMeters>1.9415363</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>147</Value>
            </HeartRateBpm>
            <SensorState>Absent</SensorState>
          </Trackpoint>
          <Trackpoint>
            <Time>2010-06-26T10:06:25Z</Time>
            <Position>
              <LatitudeDegrees>40.7780338</LatitudeDegrees>
              <LongitudeDegrees>-73.9665851</LongitudeDegrees>
            </Position>
            <AltitudeMeters>34.7448730</AltitudeMeters>
            <DistanceMeters>1.9993043</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>147</Value>
            </HeartRateBpm>
            <SensorState>Present</SensorState>
          </Trackpoint>
          <Trackpoint>
            <Time>2010-06-26T10:06:28Z</Time>
            <Position>
              <LatitudeDegrees>40.7780800</LatitudeDegrees>
              <LongitudeDegrees>-73.9665492</LongitudeDegrees>
            </Position>
            <AltitudeMeters>34.2641602</AltitudeMeters>
            <DistanceMeters>7.9581327</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>148</Value>
            </HeartRateBpm>
            <Cadence>1</Cadence>
            <SensorState>Present</SensorState>
            <Extensions>
              <TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Bike"/>
            </Extensions>
          </Trackpoint>
          <Trackpoint>
            <Time>2010-06-26T10:06:30Z</Time>
            <Position>
              <LatitudeDegrees>40.7781448</LatitudeDegrees>
              <LongitudeDegrees>-73.9665005</LongitudeDegrees>
            </Position>
            <AltitudeMeters>32.8222656</AltitudeMeters>
            <DistanceMeters>15.3775177</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>151</Value>
            </HeartRateBpm>
            <Cadence>42</Cadence>
            <SensorState>Present</SensorState>
            <Extensions>
              <TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Bike"/>
            </Extensions>
          </Trackpoint>
        </Track>
      </Lap>
    </Activity>
  </Activities>

</TrainingCenterDatabase>

输出

-- Biking --
Time: 2010-06-26T10:06:11Z, HR Value: 148
Time: 2010-06-26T10:06:12Z, HR Value: 148
Time: 2010-06-26T10:06:17Z, HR Value: 152
Time: 2010-06-26T10:06:24Z, HR Value: 147
Time: 2010-06-26T10:06:25Z, HR Value: 147
Time: 2010-06-26T10:06:28Z, HR Value: 148
Time: 2010-06-26T10:06:30Z, HR Value: 151