ValueError:在NMEA分析期间无法将字符串转换为float

时间:2015-12-22 11:53:23

标签: floating-point gps tuples python-3.4 nmea

我正在学习如何分析我从gps获得的数据,并且正在研究一本关于制作可视化转换脚本的书。我正在使用python34,我正在看的数据看起来像这样:

['$GPRMC', '2454', 'A', '3553.5295', 'N', '13938.657', 'E', '0', '43.1', '180700', '7.1', 'W', 'A*3F', '', '', '', '', '', '', '']
['$GPRMB', 'A', '', '', '', '', '', '', '', '', '', '', '', 'A', 'A*0B', '', '', '', '', '']
['$GPGGA', '2454', '3553.5295', 'N', '13938.657', 'E', '1', '5', '2.2', '18.3', 'M', '39', 'M', '', '*7F', '', '', '', '', '']
['$GPGSA', 'A', '3', '1', '4', '7', '16', '20', '', '', '', '', '', '', '', '3.6', '2.2', '2.7*35', '', '']
['$GPGSV', '3', '1', '9', '1', '38', '103', '37', '2', '23', '215', '0', '4', '38', '297', '37', '5', '0', '328', '00*70']
['$GPGSV', '3', '2', '9', '7', '77', '299', '47', '11', '7', '87', '0', '16', '74', '41', '47', '20', '38', '44', '43*73']
['$GPGSV', '3', '3', '9', '24', '12', '282', '00*4D', '', '', '', '', '', '', '', '', '', '', '', '']
['$GPGLL', '3553.5295', 'N', '13938.657', 'E', '2454', 'A', 'A*4F', '', '', '', '', '', '', '', '', '', '', '', '']
['$GPBOD', '', 'T', '', 'M', '', '*47', '', '', '', '', '', '', '', '', '', '', '', '', '']
['$PGRME', '8.6', 'M', '9.6', 'M', '12.9', 'M*15', '', '', '', '', '', '', '', '', '', '', '', '', '']
['$PGRMZ', '51', 'f*30', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
['$HCHDG', '101.1', '', '', '7.1', 'W*3C', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
['$GPRTE', '1', '1', 'c', '*37', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
['$GPRMC', '2456', 'A', '3553.5295', 'N', '13938.657', 'E', '0', '43.1', '180700', '7.1', 'W', 'A*3D', '', '', '', '', '', '', '']

现在我专注于$ GPGSV和$ GPRMC线。

我有一个定义是处理GPS数据,NMEA 0183格式:

def process_gps_data(data):
    """Processes GPS data, NMEA 0183 format.

Returns a tuple of arrays: latitude, longitude, velocity [km/h],
time [sec] and number of satellites.
See also: http://www.gpsinformation.org/dale/nmea.htm.
    """

    latitude  = []
    longitude = []
    velocity  = []
    t_seconds = []
    num_sats  = []

    for row in data:
        if row[0] == '$GPGSV':
            num_sats.append(float(row[3]))
        elif row[0] == '$GPRMC':
            t_seconds.append(float(row[1][0:2])*3600 + \
            float(row[1][2:4])*60+float(row[1][4:6]))
        latitude.append(float(row[3][0:2]) + \
            float(row[3][2:])/60.0)
        longitude.append((float(row[5][0:3]) + \
            float(row[5][3:])/60.0))
        velocity.append(float(row[7])*NMI/1000.0)

    return (array(latitude), array(longitude), array(velocity), array(t_seconds), array(num_sats))


(latitude, longitude, velocity, t_seconds, num_sats) = process_gps_data(y)

但似乎我一直收到这个错误:

Traceback (most recent call last):
  File "C:\Python34\nmea_practice.py", line 112, in <module>
    (latitude, longitude, velocity, t_seconds, num_sats) = process_gps_data(y)
  File "C:\Python34\nmea_practice.py", line 102, in process_gps_data
    float(row[1][2:4])*60+float(row[1][4:6]))
ValueError: could not convert string to float: 

我认为python3.x应该可以正常使用默认的浮点分区吗?

你能告诉我我应该做些什么吗?

添加信息。

我最初用逗号手动分隔csv,因为我的其余部分需要拆分数据来创建此定义:

#Counts the number of times a GPS command is observed
def list_gps_commands(data):
    """Counts the number of times a GPS command is observed.

Returns a dictionary object."""

    gps_cmds = dict()
    for row in data:

        try:
            gps_cmds[row[0]] += 1 
        except KeyError:
            gps_cmds[row[0]] = 1

    return gps_cmds

print(list_gps_commands(x))
print ("- - - - - - - - - - - - -")

在我手动用逗号分隔之前,原始数据看起来像这样:

['$GPRMC,002454,A,3553.5295,N,13938.6570,E,0.0,43.1,180700,7.1,W,A*3F']

有关如何“拆分”以满足'list_gps_commands并解决浮动错误的任何建议?

添加信息2

我得到的是来自我的gps设备的原始nmea数据。现在恰好是Garmin_etrex_summit。我在csv中有数据,每行的数据是一个长字符串。如果您可以继续查看$ GPRMC行的格式,您将看到共有13个字段以逗号分隔。

$GPRMC,002454,A,3553.5295,N,13938.6570,E,0.0,43.1,180700,7.1,W,A*3F   

每个字段代表某种东西,应该是指定的格式。

RMC Data Format

row[1] = hhmmss.sss (UTC Time) 
row[3] = ddmm.mmmm (Latitude)
row[5] = ddmm.mmmm (Longitude)
row[7] = anything with decimal place (Velocity in knots)

1 个答案:

答案 0 :(得分:1)

根据您的数据,row[1]'2454',但是,您尝试从4到6获得切片:

float(row[1][4:6])

返回一个空字符串。 float不知道如何处理它:

In [6]: float('')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ddb0cdb80cb6> in <module>()
----> 1 float('')

ValueError: could not convert string to float:

如果您截断了数据,可以使用前导零手动扩展它:

In [11]: '2454'.zfill(6) # When `'2454'` is a `str`
Out[11]: '002454'

In [12]: '{:06}'.format(2454) # Another way
Out[12]: '002454'

In [13]: float('{:06}'.format(2454)[4:6])
Out[13]: 54.0

<强> UPD

好像您的数据可以解析为常规csv

In [17]: list(csv.reader(['$GPRMC,002454,A,3553.5295,N,13938.6570,E,0.0,43.1,180700,7.1,W,A*3F']))
Out[17]: 
[['$GPRMC',
  '002454',
  'A',
  '3553.5295',
  'N',
  '13938.6570',
  'E',
  '0.0',
  '43.1',
  '180700',
  '7.1',
  'W',
  'A*3F']]

正如您所看到的,002454解析没有任何问题,您不必使用前导零来扩展它。