Anybody know Python?

Discussion in 'Computing, Science, and Technology' started by simonrichards150, Nov 26, 2008.

  1. simonrichards150

    simonrichards150 New Member

    Joined:
    Oct 29, 2007
    Messages:
    2,058
    Likes Received:
    94
    Device:
    iPod touch
    Does anyone know how to split strings in python?

    I am writing code for a GPS/GPRS module, and the GPS spits out NMEA data in sentences like this:

    Code:
    $GPGGA,065049.000,[COLOR="Red"]4542.8078,N,01344.2698,E[/COLOR],1,07,1.2,268.1,M,45.2,M,,0000*5F
    The sections in red are the latitude and longitude, in degrees and minutes, but nearly all mapping software wants a decimal value.

    I can convert to decimal no problem, but I need to separate the parts in red from the rest of the sentence so that I can convert them separately.

    Anyone know how to do this with Python?
  2. 101gfx

    101gfx New Member

    Joined:
    Jun 6, 2008
    Messages:
    349
    Likes Received:
    0
    Device:
    iPod touch
    http://www.python.org/doc/2.5.2/lib/string-methods.html

    EDIT:
    split( [sep [,maxsplit]])
    Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made). Consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1„2'.split(',')"returns "['1', '', '2']"). The sep argument may consist of multiple characters (for example, "'1, 2, 3'.split(', ')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']".

    If sep is not specified or is None, a different splitting algorithm is applied. First, whitespace characters (spaces, tabs, newlines, returns, and formfeeds) are stripped from both ends. Then, words are separated by arbitrary length strings of whitespace characters. Consecutive whitespace delimiters are treated as a single delimiter ("'1 2 3'.split()" returns "['1', '2', '3']"). Splitting an empty string or a string consisting of just whitespace returns an empty list.
  3. simonrichards150

    simonrichards150 New Member

    Joined:
    Oct 29, 2007
    Messages:
    2,058
    Likes Received:
    94
    Device:
    iPod touch
    Thanks, I will just check to see if that is supported by the module (Y)

    EDIT: It seems to be supported, thanks

Share This Page