py-serial + CSV

Steve Holden steve at holdenweb.com
Tue Aug 23 20:24:08 CEST 2005


McBooCzech wrote:
> Sorry, I did not mentioned the data flow from the serial port is
> permanent/continuous (as long as the GPS receiver is connected to the
> serial port). The input data are commning every second, they are comma
> separated and they are looking like:
> 
> $GPGGA,174525.617,5026.1080,N,01521.6724,E,1,05,1.8,306.5,M,,,,0000*0B
> $GPGSA,A,3,02,09,05,06,14,,,,,,,,3.6,1.8,3.1*31
> $GPGSV,3,1,09,30,74,294,,05,65,093,49,06,40,223,32,02,39,089,49*78
> $GPRMC,174525.617,A,5026.1080,N,01521.6724,E,0.0,005.8,230805,,*0A
> etc....
> 
>>From the rows they are begining with $GPRMC I want to get following
> data only (positions 2,4,6)
> 174525.617
> 5026.1080
> 01521.672
> 
> This (according to your suggestions) is my code which works for me
> 
> import serial
> s = serial.Serial(port=0,baudrate=4800, timeout=20)
> while 1:
> 	line = s.readline()
> 	words = line.split(',')
> 	if words[0]=="$GPRMC":
> 		print words[1], words[3], words[5]
> 
> I just wonder if there is some beter (or as you are saying "more
> pythonic":) aproach how to write such a piece of code.

That code is quite tidy. You could save yourself the split on lines that 
weren't of interest, though frankly this isn't essential - this task 
won't use 1% of CPU on almost any computer built in the last five years. 
But, if you are interested in seeing other solutions you might consider 
it, and it does avoid the split when it's not necessary.

while 1:
     line = s.readline()
     if line.startswith("$GPRMC"):
         words = line.split(",")
         print words[1], words[3], words[5]

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list