Python+将gpx转换为cs

2024-05-23 14:45:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.gpx文件的列表,我想用python将其转换为csv。 我有以下内容

#Set the working directory to the INDIR variable
os.chdir(INDIR)

def parsegpx(f):
    #Parse a GPX file into a list of dictoinaries.  
    #Each dict is one row of the final dataset

    points2 = []
    with open(f, 'r') as gpxfile:
        # print f
        gpx = gpxpy.parse(gpxfile)
        for track in gpx.tracks:
            for segment in track.segments:
                for point in segment.points:
                    dict = {'Timestamp' : point.time,
                            'Latitude' : point.latitude,
                            'Longitude' : point.longitude,
                            'Elevation' : point.elevation
                            }
                    points2.append(dict)
    return points2   

#Parse the gpx files into a pandas dataframe
dirs = os.listdir(INDIR)
df2 = pd.concat([pd.DataFrame(parsegpx(f)) for f in dirs], keys=files)

但是,如果运行该代码,则会出现以下错误: UnicodeDecodeError:“utf-8”编解码器无法解码位置3131中的字节0x80:起始字节无效

有人能帮我吗?我试图在open中添加编码拉丁语-1,但它不起作用。在

谢谢, 联邦快递


Tags: oftheinforparseosgpxopen
1条回答
网友
1楼 · 发布于 2024-05-23 14:45:31

我们能看看错误发生在哪里的gpx文件吗?我用你的代码片段尝试了文件https://github.com/tkrajina/gpxpy/blob/master/test_files/cerknicko-jezero-no-creator.gpx

import gpxpy

def parsegpx():
    points2 = list()
    with open('cerknicko-jezero-no-creator.gpx','r') as gpxfile:
        gpx = gpxpy.parse(gpxfile)
        for track in gpx.tracks:
            for segment in track.segments:
                for point in segment.points:
                    dict = {'Timestamp': point.time,
                            'Latitude': point.latitude,
                            'Longitude': point.longitude,
                            'Elevation': point.elevation
                            }
                    points2.append(dict)
    print(points2)

if __name__ == '__main__':

    parsegpx()

得到正确的结果:

^{pr2}$

相关问题 更多 >