Created
January 29, 2015 11:25
-
-
Save stevedoyle/6d1784e308822ce82a36 to your computer and use it in GitHub Desktop.
Split TCX file with laps into multiple files, with 1 lap per file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import os | |
from lxml import objectify | |
namespace = 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2' | |
def remove_laps(): | |
pass | |
def count_laps(tree_root): | |
return int(tree_root.xpath('count(//ns:Lap)', namespaces={'ns': namespace})) | |
def generate_filename_for_lap(base_filename, lap_num): | |
parts = os.path.splitext(base_filename) | |
return parts[0] + '_' + str(lap_num+1) + parts[1] | |
def remove_laps_except(tree, lap_num): | |
laps = tree.getroot().xpath('//ns:Lap', namespaces={'ns': namespace}) | |
del laps[lap_num] | |
for lap in laps: | |
lap.getparent().remove(lap) | |
def tcxsplit(filename): | |
tree = objectify.parse(filename) | |
root = tree.getroot() | |
num_laps = count_laps(root) | |
print('%d laps found in %s' % (num_laps, filename)) | |
for lap_idx in range(num_laps): | |
lap_tree = copy.deepcopy(tree) | |
remove_laps_except(lap_tree, lap_idx) | |
lap_filename = generate_filename_for_lap(filename, lap_idx) | |
print('Creating ' + lap_filename) | |
lap_tree.write(lap_filename, pretty_print=True) | |
if __name__ == '__main__': | |
tcxsplit('sample.tcx') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment