Parsing GPS receiver output via regex in Python

后端 未结 7 1305
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 11:01

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 weat

7条回答
  •  温柔的废话
    2020-12-14 11:31

    Those are comma separated values, so using a csv library is the easiest solution.

    I threw that sample data you have into /var/tmp/sampledata, then I did this:

    >>> import csv
    >>> for line in csv.reader(open('/var/tmp/sampledata')):
    ...   print line
    ['$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']
    

    You can then process the data however you wish. It looks a little odd with the '**' at the start and end of some of the values, you might want to strip that stuff off, you can do:

    >> eastwest = 'E**'
    >> eastwest = eastwest.strip('*')
    >> print eastwest
    E
    

    You will have to cast some values as floats. So for example, the 3rd value on the first line of sample data is:

    >> data = '**4250.5589'
    >> print float(data.strip('*'))
    4250.5589
    

提交回复
热议问题