Pynmea2属性错误

时间:2015-04-25 00:59:23

标签: python-2.7

我一直在使用pynmea2库,但今天我通过它运行了一条GPGGA消息,并且在尝试访问对象datetime方法时抛出了属性错误。

>>> from pynmea2 import parse
>>> a = '$GPGGA,201326.000,3348.5072,N,11809.6409,W,2,20,0.55,37.5,M,-34.3,M,0000,0000*65'
>>> msg = parse(a)
>>> msg
<GGA(timestamp=datetime.time(20, 13, 26), lat='3348.5072', lat_dir='N', lon='11809.6409', lon_dir='W', gps_qual='2', num_sats='20', horizontal_dil='0.55', altitude=37.5, altitude_units='M', geo_sep='-34.3', geo_sep_units='M', age_gps_data='0000', ref_station_id='0000')>
>>> msg.datetime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/pynmea2/nmea.py", line 154, in __getattr__
    raise AttributeError(name)
AttributeError: datetime

这是第154行和nmea.py中的所有相关状态:

def __getattr__(self, name):
    #pylint: disable=invalid-name
    t = type(self)
    try:
        i = t.name_to_idx[name]
    except KeyError:
        raise AttributeError(name)
    f = t.fields[i]
    if i < len(self.data):
        v = self.data[i]
    else:
        v = ''
    if len(f) >= 3:
        if v == '':
            return None
        return f[2](v)
    else:
        return v

任何想法可能是什么? 谢谢你看这个!

2 个答案:

答案 0 :(得分:0)

找到它... GPGGA句子的字符串中没有日期值。

答案 1 :(得分:0)

我认为你想要使用:

访问GPGGA记录的timestamp属性
>>> from pynmea2 import parse
>>> a = '$GPGGA,201326.000,3348.5072,N,11809.6409,W,2,20,0.55,37.5,M,-34.3,M,0000,0000*65'
>>> msg = parse(a)
>>> msg
<GGA(timestamp=datetime.time(20, 13, 26), lat='3348.5072', lat_dir='N', lon='11809.6409', lon_dir='W', gps_qual='2', num_sats='20', horizontal_dil='0.55', altitude=37.5, altitude_units='M', geo_sep='-34.3', geo_sep_units='M', age_gps_data='0000', ref_station_id='0000')>
>>> msg.timestamp
datetime.time(20, 13, 26)