[FRPythoneers] Question about string.join method

Evelyn Mitchell efm at tummy.com
Mon Apr 30 15:18:36 MDT 2001


Well, the docs for string.join do describe the behavior you're seeing:

join(words[, sep]) 
       Concatenate a list or tuple of words with intervening occurrences 
		of sep. The default value for sep is a single space character. It is 
		always true that "string.join(string.split(s, sep), sep)" equals s. 

But it's not easy to find the right way to do it (look for 
'string literal concatenation' for the compile time docs, which 
mention briefly the run time '+' convention).

Use of the '+' operator to concatenate strings mentioned in the 
tutorial, though. (But not close to the word 'join', instead
it's called 'glued together', making it impossible to find if
you're looking for 'join string').

So, the answer is everything is working as it is supposed to, but
the docs could be a lot clearer. I've cc'd python-docs at python.org
to bring them into the loop.

Evelyn Mitchell
efm at tummy.com


On Mon, Apr 30, 2001 at 11:30:39AM -0600, J. Wayde Allen wrote:
> 
> Friday I was tinkering with the following piece of code to extract
> latitude, longitude, time, and elevation data from the NMEA data stream
> put out by a GPS receiver.  What I have below seems to work
> fine.  However, I had originally thought that the line
> 
>            self.latitude = field[2] + field[3]
> 
> should read
> 
>            self.latitude = string.join(field[2], field[3])
> 
> When I do that the numbers in field[2] = "4009.120" and the letter
> in field[3] = "N" get interleaved resulting in 
> 
>            self.latitude == "4N0N0N9N.N1N2N0"
> 
> I had thought that the strin.join method was supposed to append these
> fields together with an intermediate space.  The desired result would have
> been
> 
>            self.latitude == "4009.120 N"
> 
> So, what am I missing here?  Adding the strings as I've done seems to
> work, but I'm not sure why?
> 
> - Wayde
>   (wallen at lug.boulder.co.us)
> ----------------------------------------------------------------------------
> 
> #!/usr/bin/env python
> 
> import string
> 
> class nmeafile:
> 
>    def __init__(self, filename):
>       self.filename = filename
> 
>       for line in open(self.filename, 'r').readlines():
>          if line[:6] == '$GPGGA':
>             field = string.split(line,",")
>             self.time = field[1]
>             self.latitude = field[2] + field[3] 
>             self.longitude = field[4] + field[5]
>             self.quality = field[6]
>             self.num_svs = field[7]
>             self.hdop = field[8]
>             self.altitude = field[9] + field[10]
> 
>             print self.time, self.latitude, self.longitude, self.altitude
>             
>          
> if __name__ == '__main__':
>    
>    print 'Time Latitude Longitude Elevation'   
>    gps = nmeafile('gpsdatafile')
>             
> 
> 
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers



More information about the FRPythoneers mailing list