Bullet proof passing numeric values from NMEA data stream.

Steve Holden steve at holdenweb.com
Tue Mar 20 13:38:03 CET 2007


Doug Gray wrote:
> Folks,
> I am looking for a fast but most importantly a bullet proof method to pass
> and NMEA data stream (GPS output) ascii numeric strings. The best I can
> offer is:
> 
> def fint(a):
>  try: return int(float(a))
>  except: return 0
> 
> The reason for this is the quality of the data from the huge variety of
> GPS units available varies considerably.  Some units do not follow the
> standard and I want to pass the data as best I can without hanging the
> code for an oddball data value.
> 
> Can anyone suggest better?
> 
> For example, each of the following throw the exception so do not return
> the correct value:
> 
> int('00.')
> int(' 00.')
> float('-  00')
> float(' -  00')
> float(' -  00')
> float(' -  00.')
> float('-  00.')
> float('-  10.')
> float('- 10.')
> float('- 10.')
> int('- 10.')
> int('- 10.')
> float('- 10.')
> int('1.0')
> 
> Also, why should I consider the string module?  Is it faster/better?
> 
> TIA,
> Doug

Try something like

def fint(s):
   return float(s.replace(" ", ""))

I really don't think it's a good idea to silently ignore conversion 
errors in GPS positioning.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list