A complete and utter starter Python script

A friend said that it was the duty of a computer scientist to learn a new computer language a year and asked me to introduce him to Python.

Ignoring the fact that he’ll probably outstrip my two-year-old Python abilities in about a week, I’ll drop one of my first Python scripts here in case he or anyone else learns something.

I learnt Python in the first place to solve a problem I was having getting a GPS to talk to Pure Data (pd), namely that pd was not dealing with string splitting and parsing very well. This first script reflects that by having an NMEA sentence as its example but it could be any comma separated string.

#!/usr/bin/env python 
# splittest.py
#
# A very simple python script to show string splitting to
# a set of named variables
#
# One of the first scripts I wrote to get my head around how you might
# begin to parse an NMEA sentence from a GPS
# 
# Put this anywhere, call it splittest.py and run from the terminal
# with $ python splittest.py
#
# Daniel Belasco Rogers 2008, 2011

# declare string
line = '$GPSGV, 234932, A, 2345999940, 234, 9, 23, 593'

# print it to the terminal
print 'original string:'
print line
print

# assign a set of variables
header, one, two, three, four, five, six, seven = line.split(',')

# print these
print 'list of variables:'
print header, one, two, three, four, five, six, seven

# as a separate excercise, show that .split makes a list and iterate
# over the list

stringlist = line.split(',')

# notice the \n newline character at the beginning of the print string
# to force a newline first
print '\nstringlist =', stringlist 

# iterate over list and print each item
print '\nlist of items:'
for item in stringlist:
    print item
This entry was posted in Code, GPS, Python. Bookmark the permalink.