在 Python 中通过正则表达式解析 GPS 接收器输出接收器、正则表达式、Python、GPS

2023-09-07 04:16:12 作者:思念瘦瘦的

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

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.

虽然我自己有点(永恒的)初学者,但我比我的朋友有更多的编程经验.因此,当他向我寻求建议时,我说服他用我选择的语言 Python 编写程序.

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.

在项目的这一点上,我们正在处理解析来自 GPS 设备的输入的代码.这是一些示例输入,我们需要提取的数据以粗体显示:

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

$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."

两天前,我的朋友能够从用于跟踪最近气象气球发射的 GPS 接收器获取完整日志.数据比较长,所以我都放在这个pastebin中.

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)