ValueError: could not convert string to float during NMEA analysis
ValueError: could not convert string to float during NMEA analysis
我正在学习如何分析从我的 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']
关于如何 'split' 足以满足 '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
每个字段代表某种东西,应该采用指定的格式。
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)
根据您的数据,row[1]
是 '2454'
,但是,您正在尝试从 4 到 6 切片:
float(row[1][4:6])
其中returns一个空字符串。 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
解析没有任何问题,您不必使用前导零展开它。
我正在学习如何分析从我的 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']
关于如何 'split' 足以满足 '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
每个字段代表某种东西,应该采用指定的格式。
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)
根据您的数据,row[1]
是 '2454'
,但是,您正在尝试从 4 到 6 切片:
float(row[1][4:6])
其中returns一个空字符串。 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
解析没有任何问题,您不必使用前导零展开它。