解析GPX文件时出现隐秘错误消息

时间:2016-08-01 21:20:40

标签: python xml python-3.x parsing

在上一个问题中,我询问了Python 3.5程序中的问题,该程序将多个gpx轨道合并为一个klm文件。我没有收到答案,我决定将该bug作为一个可选功能。

今天,我完成了程序的GUI编程,并开始使用其他(较旧的)gpx文件进行测试,我从计算机中找到了这些文件。在运行程序时,会出现此错误消息,但它不会破坏程序的工作。

  ERROR:root:unbound prefix: line 14, column 0
  Traceback (most recent call last):
  File "C:\Program Files\Python35\lib\site-packages\gpxpy\parser.py", line 188, in parse
    self.xml_parser = XMLParser(self.xml)
  File "C:\Program Files\Python35\lib\site-packages\gpxpy\parser.py", line 44, in __init__
    self.dom = mod_minidom.parseString(xml)
  File "C:\Program Files\Python35\lib\xml\dom\minidom.py", line 1968, in parseString
    return expatbuilder.parseString(string)
  File "C:\Program Files\Python35\lib\xml\dom\expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "C:\Program Files\Python35\lib\xml\dom\expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: unbound prefix: line 19, column 0

通过反复试验,我发现它是由gpxpy.parse(gpx_file)引起的,这会引发gpxpy.parser.mod_gpx.GPXXMLSyntaxException。由于我将它用作错误异常,该错误将返回函数,而父循环尝试解析列表中的下一个文件。导致异常的GPX跟踪被跳过。

class funs: #Class for gruping custom functions
    #... Other functions
    def merg2(adr):
        try:
            gpx_file = open(adr, 'r')
        except FileNotFoundError:
            return
        try:
            gpx = gpxpy.parse(gpx_file) #Raises error and returns
        except gpxpy.parser.mod_gpx.GPXXMLSyntaxException:
            return
        #... Process gpx
    #...
for i in o2: # o2 is list of files with full path
    infow(2) # GUI progress-bar updater
    try:
        funs.merg2(i) #Not merging. Just load tracks to graph
        exit() #Used for debugging only
    except UnicodeDecodeError:
        pass    #Files may get damaged over time

我不知道导致该错误的原因是什么,也不知道输入文件中的哪些行。每个文件都会发生跟踪,无法解析,但最后一行消息中的行号有时会有所不同。它可以是14,18,19或22。

这个错误是什么意思?究竟是什么原因导致错误?怎么可以避免?我应该编辑gpx track,脚本还是忽略错误?

PS。我尝试导入的所有GPX曲目都是由gpxpy模块本身生成的。

编辑:这是两个gpx文件的开头。第一个原始,然后由gpxpy生成。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<gpx version="1.1" creator="Locus Map, Android"
 xmlns="http://www.topografix.com/GPX/1/1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
 xmlns:gpx_style="http://www.topografix.com/GPX/gpx_style/0/2"
 xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
 xmlns:gpxtrkx="http://www.garmin.com/xmlschemas/TrackStatsExtension/v1"
 xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v2"
 xmlns:locus="http://www.locusmap.eu">
    <metadata>
        <desc>File with points/tracks from Locus Map Pro/3.16.2</desc>
    </metadata>
<trk>
<name>2015-12-27.Lohusalus.jalutamas</name>
    <extensions>
        <gpx_style:line>
            <gpx_style:color>#960000FF</gpx_style:color>
            <gpx_style:width>4.0</gpx_style:width>
        </gpx_style:line>
    </extensions>
<trkseg>

修改过的文件:

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="Locus Map, Android">
<metadata>
<desc>File with points/tracks from Locus Map Pro/3.16.2</desc>
<author>
<link ></link></author>
<copyright ></copyright>
<link ></link></metadata>
<trk>
<name>2015-12-27.Lohusalus.jalutamas</name>
<desc> </desc>
<link ></link>
<extensions>
<gpx_style:line>
            </gpx_style:line></extensions>
<trkseg>

我可以做些什么来阻止gpxpy gpx.to_xml()定义线条样式?

0 个答案:

没有答案