使用Python中的正则表达式解析GPS接收器输出 [英] Parsing GPS receiver output via regex in Python

查看:191
本文介绍了使用Python中的正则表达式解析GPS接收器输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一位朋友正在完成他的航空航天工程硕士学位。对于他的最后一个项目,他是一个小团队,负责编写跟踪天气气球,火箭和卫星的计划。该程序接收来自GPS设备的输入,对数据进行计算,并使用这些计算的结果来控制一系列设计用于定向定向通信天线的电机,因此气球,火箭或卫星总是保持聚焦。 p>

虽然我自己有点(永恒)初学者,但我的编程经验比我的朋友多。所以当他向我咨询时,我说服他用Python编写程序,我选择的语言。



在这个项目中,我们正在研究代码解析来自GPS设备的输入。以下是一些示例输入,我们需要以粗体提取数据:

$ GPRMC,092204.999, 4250.5589,S,14718.5084,E ,1,12,24.4,89.6,M 1,0000 * 1F
$ GPRMC,093345.679,4234.7899,N,11344.2567,W 3,22 ,24.5,1000.23,M,0000 * 1F
$ GPRMC,044584.936,1276.5539,N,88734.1543,E,2,04,33.5, 600.323,M *,* 00
$ GPRMC,199304.973,3248.7780,N,11355.7832,W,1,06,02.2,25722.5, M,* 00
$ GPRMC,066487.954,4572.0089,S,45572.3345,W 3,09,15.0,35000.00,M,以下是对这些数据的进一步解释:


我看起来每条线都需要5件
,并记住
这些区域中的任何一个可能是
空的,意思是只有两个
逗号彼此相邻,这样
作为',,,'有两个字段可能是
随时充满。其中一些只有
有两个或三个选项,它们可能是
,但我认为我不应该指望




两天前,我的朋友能够从GPS接收机获取完整的日志,用于追踪最近发布的气象气球,数据相当长,所以我把它放在<一个href =http://pastebin.com/f5f5cf9ab =noreferrer>这个pastebin 。



我对于正则表达式还是比较新的我自己,所以我正在寻找一些援助。

解决方案

分裂应该做的伎俩。这是一个很好的方式来提取数据, :

 >>> line =$ GPRMC,199304.973,3248.7780,N,11355.7832,W,1 ,06,02.2,25722.5,M ,,, * 00
>>> line = line.split(,)
>>> neededData =(float(line (2)),line [3],float(line [4]),line [5],float(line [9]))
>>>>>> 'N',11355.7832,'W ',25722.5)


I have a friend who is finishing up his masters degree in aerospace engineering. For his final project, he is on a small team tasked with writing a program for tracking weather balloons, rockets and satellites. The program receives input from a GPS device, does calculations with the data, and uses the results of those calculations to control a series of motors designed to orientate a directional communication antenna, so the balloon, rocket or satellite always stays in focus.

Though somewhat of a (eternal) beginner myself, I have more programming experience than my friend. So when he asked me for advice, I convinced him to write the program in Python, my language of choice.

At this point in the project, we are working on the code that parses the input from the GPS device. Here is some example input, with the data we need to extract in bold:

$GPRMC,092204.999,4250.5589,S,14718.5084,E,1,12,24.4,89.6,M,,,0000*1F $GPRMC,093345.679,4234.7899,N,11344.2567,W,3,02,24.5,1000.23,M,,,0000*1F $GPRMC,044584.936,1276.5539,N,88734.1543,E,2,04,33.5,600.323,M,,,*00 $GPRMC,199304.973,3248.7780,N,11355.7832,W,1,06,02.2,25722.5,M,,,*00 $GPRMC,066487.954,4572.0089,S,45572.3345,W,3,09,15.0,35000.00,M,,,*1F

Here is some further explanation of the data:

"I looks like I'll need five things out of every line. And bear in mind that any one of these area's may be empty. Meaning there will be just two commas right next to each other. Such as ',,,' There are two fields that may be full at any time. Some of them only have two or three options that they may be but I don't think I should be counting on that."

Two days ago my friend was able to acquire the full log from the GPS receiver used to track a recent weather balloon launch. The data is quite long, so I put it all in this pastebin.

I am still rather new with regular expressions myself, so I am looking for some assistance.

解决方案

splitting should do the trick. Here's a good way to extract the data, as well:

>>> line = "$GPRMC,199304.973,3248.7780,N,11355.7832,W,1,06,02.2,25722.5,M,,,*00"
>>> line = line.split(",")
>>> neededData = (float(line[2]), line[3], float(line[4]), line[5], float(line[9]))
>>> print neededData
(3248.7779999999998, 'N', 11355.7832, 'W', 25722.5)

这篇关于使用Python中的正则表达式解析GPS接收器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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