Tapping Python Listings
>>> # create a new list
>>> a = ['zero','one','two','three']
>>> # indexing is zero-based
>>> print a[0]
'zero'
>>> # iterate through the list, print out members
>>> for b in a:
...     print b
...
'zero'
'one'
'two'
'three'

Listing 1: Iterating through a list


>>> # create a new dictionary object
>>> #  note that here our keys are integers and values are strings
>>> #  but this doesn't always have to be the case
>>> d = {1:'Urban',
...      2:'Wooded',
...      3:'Marsh',
...      4:'Pasture'}
>>> d
{1: 'Urban', 2: 'Wooded', 3: 'Marsh', 4: 'Pasture'}
>>> # access the value for key 2
>>> d[2]
'Wooded'
>>> # Create a new empty list object
>>> l=[]
>>> # use a list comprehension to iterate through the dictionary
>>> #  items and append the values to list l
>>> for k,v in d.iteritems():
...     l.append(v)
>>> # print out list l
>>> l
['Urban', 'Wooded', 'Marsh', 'Pasture']
>>> # or just use the built-in methods to get the keys
>>> d.keys()
[1, 2, 3, 4]
>>> # or to get the values
>>> d.values()
['Urban', 'Wooded', 'Marsh', 'Pasture']
>>> # test for existence of a key
>>> d.has_key('5')
False
>>> # likewise
>>> 1 in d
True 

Listing 2: Working with a dictionary

>>> # in its simplest form - fetch one file and save it locally
>>> import urllib
>>> urllib.urlretrieve('http://www.fhwa.dot.gov/bridge/nbi/2007/AR07.txt','C:/testing/nbi/ar07.txt')
('C:/testing/nbi/ar07.txt', <httplib.HTTPMessage instance at 0x733e68>)
>>> # more advanced, iterate through a list to fetch multiple files
>>> url = 'http://www.fhwa.dot.gov/bridge/nbi/2007/'
>>> # let's grab 3 files - Arkansas, Oklahoma, and Missouri
>>> files = ['ar','ok','mo']
>>> for file in files:
...     urllib.urlretrieve(url + file + '07.txt', 'C:/testing/nbi/' + file + '07.txt')
...
('C:/testing/nbi/ar07.txt', <httplib.HTTPMessage instance at 0x73a418>)
('C:/testing/nbi/ok07.txt', <httplib.HTTPMessage instance at 0x73a0d0>)
('C:/testing/nbi/mo07.txt', <httplib.HTTPMessage instance at 0x73a418>)

Listing 3: Fetching files from the Web

>>> # open the text file, read all of the lines into memory
>>> file = open('C:/testing/simple.txt').read().splitlines()
>>> # print file contents to stdout line by line
>>> for line in file:
...     print line
...
This is line one.
This is line two.
This is line three.
This is line four.
This is line five.
Listing 4: Reading a simple text file

>>> # import csv module
>>> import csv
>>> # open and read the csv file into memory
>>> file = open('C:/testing/simple-csv.csv')
>>> reader = csv.reader(file)
>>> # iterate through the lines and print them to stdout
>>> # the csv module returns us a list of lists and we
>>> #  simply iterate through it
>>> for line in reader:
...     print line
...
['34.79038', '-96.80871', '4/13/1983']
['34.93032', '-96.44490', '2/5/1967']
['34.95507', '-96.92268', '12/23/2001']
['34.95689', '-96.92263', '8/9/1999']
['34.92559', '-96.68021', '8/25/1954']
Listing 5: Reading a simple CSV file


# open the text file, read it into memory
file = open('c:/testing/a-little-more-involved.txt').read().splitlines()
# define a new, empty list object
l = []
# iterate through the lines of the file, slice up the line by index
#   position of each data column
for line in file:
    v0 = line[0:3]
    v1 = line[3:18].strip()
    v2 = line[18:19]
    v3 = line[19:20]
    v4 = line[20:21]
    v5 = line[21:26]
    v6 = line[26:27]
    v7 = line[27:29]
    # add the parsed items to list l; as we go through the lines,
    #   we create a nested list
    l.extend([[v0,v1,v2,v3,v4,v5,v6,v7]])
# iterate through list, print out members
for each in l:
    print each
	
['056', '10616', '1', '6', '1', '00300', '0', '09']
['014', '10617', '1', '6', '1', '00300', '0', '09']
['056', '000000000000263', '1', '2', '1', '00071', '0', '03']
['056', '000000000000264', '1', '2', '1', '00071', '0', '03']
['020', '000000000000279', '1', '4', '1', '00721', '0', '03']
['069', '000000000000284', '1', '2', '1', '00070', '0', '03']
['056', '000000000000286', '1', '2', '1', '00070', '0', '03']
['088', '000000000000305', '1', '2', '6', '00167', '0', '07']
['056', '000000000000306', '1', '2', '1', '00167', '0', '07']
['069', '000000000000307', '1', '2', '1', '00167', '0', '07']
	
Listing 6: Parsing a text file by index position

# define our function
def getState( st_code ):
    """ Maps a state name to a state code """
    dict = {'014':'Alabama',
            '020':'Alaska',
            '049':'Arizona',
            '056':'Arkansas',
            '069':'California',
            '088':'Colorado'}
    # test to see if state code exists as a key in dict
    if st_code in dict.keys():
        # if so, then get the value (state name)
        state = dict[st_code]
    else:
        # if not, map code to unknown
        state = 'UNKNOWN'
    # return the state name
    return state

# open the text file, read it into memory
file = open('c:/testing/a-little-more-involved.txt').read().splitlines()
# define a new, empty list object
l = []
# iterate through the lines of the file, slice up the line by index
#   position of each data column
for line in file:
    # call our function
    v0 = getState( line[0:3] )
    v1 = line[3:18].strip()
    v2 = line[18:19]
    v3 = line[19:20]
    v4 = line[20:21]
    v5 = line[21:26]
    v6 = line[26:27]
    v7 = line[27:29]
    # add the parsed items to list l; as we go through the lines,
    #   we create a nested list
    l.extend([[v0,v1,v2,v3,v4,v5,v6,v7]])
# iterate through list, print out members
for each in l:
    print each

['Arkansas', '10616', '1', '6', '1', '00300', '0', '09']
['Alabama', '10617', '1', '6', '1', '00300', '0', '09']
['Arkansas', '000000000000263', '1', '2', '1', '00071', '0', '03']
['Arkansas', '000000000000264', '1', '2', '1', '00071', '0', '03']
['Alaska', '000000000000279', '1', '4', '1', '00721', '0', '03']
['California', '000000000000284', '1', '2', '1', '00070', '0', '03']
['Arkansas', '000000000000286', '1', '2', '1', '00070', '0', '03']
['Colorado', '000000000000305', '1', '2', '6', '00167', '0', '07']
['Arkansas', '000000000000306', '1', '2', '1', '00167', '0', '07']
['California', '000000000000307', '1', '2', '1', '00167', '0', '07']

Listing 7: Parsing a text file and using a dictionary to decipher coded values

# open the text file, read it into memory
file = open('c:/testing/lat-lon-date-conversions.txt').read().splitlines()
# define a new, empty list object
l = []
# iterate through the lines of the file, slice up the line by index
#   position of each data column
for line in file:
    v1 = line[0:8].strip()  # lat dms
    v2 = v1[0:2]  # lat deg
    v3 = v1[2:4]  # lat min
    v4 = v1[4:6]+'.'+v1[6:8]  # lat sec
    v5 = str(int(v2)+(float(v3)/60)+(float(v4)/3600))  # lat dd
    v6 = line[8:17].strip()  # lon dms
    v7 = v6[0:3]  # lon deg
    v8 = v6[3:5]  # lon min
    v9 = v6[5:7]+'.'+v6[7:9]  # lon sec
    v10 = str('-'+str(int(v7)+(float(v8)/60)+float(v9)/3600))  # lon dd
    v11 = line[17:21]  # date as MMYY
    v12 = str(v11[0:2])+'/'+str(v11[2:4])  # concatenate date as MM/YY
    # add our line to list l
    # do string formatting to lat/long to display only 5 decimal points
    l.extend([[('%.5f' % float(v5)),('%.5f' % float(v10)),v12]])
    # print out each line to stdout
for each in l:
    print each
    
['36.44000', '-94.03667', '06/94']
['36.44000', '-94.04833', '06/94']
['33.09167', '-93.90333', '09/05']
['33.02667', '-93.89167', '07/05']
['34.34111', '-93.50056', '05/06']
['34.24833', '-93.67667', '01/07']
['34.22500', '-93.89500', '07/06']
['33.76833', '-92.48500', '07/06']
['33.74500', '-92.47667', '07/06']
['33.68000', '-92.46667', '07/06']

Listing 8: Reformatting data in-line

import arcgisscripting, csv, os
gp = arcgisscripting.create()

# Here we have three custom functions we will call later in our code

def killObject( object ):
    """ Kills an input object """
    if gp.Exists(object):
        gp.Delete_management(object)

def makeFgdb( dir, db, fd, spatRef ):
    """ Create a file geodatabase and featuredataset """
    if os.path.isdir(dir) != 1:
        os.mkdir(dir)
    killObject( os.path.join(dir, db) )
    gp.CreateFileGDB_management(dir, db)
    gp.CreateFeatureDataset_management(os.path.join(dir, db), fd, spatRef)
    
def createGdbTable( db, table, fields ):
    """ Creates an empty standalone GDB table and adds fields provided in a list 
        - with a set schema """
    killObject(db + '/' + table)
    gp.CreateTable(db, table)
    for field in fields:
        if field[1] == 'TEXT':
            gp.AddField_management(db + '/' + table,field[0],field[1],'#','#',field[2],field[3],'NULLABLE','NON_REQUIRED','#')
        else:
            gp.AddField_management(db + '/' + table,field[0],field[1],'#','#','#',field[3],'NULLABLE','NON_REQUIRED','#')

# our spatial reference, this can be copied from a prj file
sr = 'GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982]],\
      PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
# our list of fields and their properties:
#   [field_name,field_type,field_length (zero for non-text fields),field_alias]
fields = [['LAT','DOUBLE','0','Latitude'],
          ['LON','DOUBLE','0','Longitude'],
          ['SOMEDATE','TEXT','6','Some Date']]
          
# create our new file geodatabase to work with
makeFgdb( 'C:/Testing', 'NBI.gdb', 'Bridges', sr )
# create our standalone geodatabase table and its schema
createGdbTable( 'C:/Testing/NBI.gdb', 'Test', fields )

# start slurping our text file into gdb table
# create a cursor on the table
rows = gp.InsertCursor('C:/Testing/NBI.gdb/Test')
# open the text file, read it into memory
file = open('c:/Testing/lat-lon-date-conversions.txt').read().splitlines()
# define a new, empty list object
l = []
# iterate through the lines of the file, slice up the line by index
#   position of each data column
for line in file:
    v1 = line[0:8].strip()  # lat dms
    v2 = v1[0:2]  # lat deg
    v3 = v1[2:4]  # lat min
    v4 = v1[4:6]+'.'+v1[6:8]  # lat sec
    v5 = str(int(v2)+(float(v3)/60)+(float(v4)/3600))  # lat dd
    v6 = line[8:17].strip()  # lon dms
    v7 = v6[0:3]  # lon deg
    v8 = v6[3:5]  # lon min
    v9 = v6[5:7]+'.'+v6[7:9]  # lon sec
    v10 = str('-'+str(int(v7)+(float(v8)/60)+float(v9)/3600))  # lon dd
    v11 = line[17:21]  # date as MMYY
    v12 = str(v11[0:2])+'/'+str(v11[2:4])  # concatenate date as MM/YY
    # add our line to list l
    # do string formatting to lat/long to only display 5 decimal points
    l.extend([[('%.5f' % float(v5)),('%.5f' % float(v10)),v12]])
ln = 0
# iterate through our data
for line in l:
    t = 0
    # create a new row
    row = rows.NewRow()
    # iterate through the fields
    for field in fields:
        val = line[t].strip()
        # set the value for each field
        if field[1] == 'DOUBLE':
            row.SetValue(field[0], '%.5f' % float(val))
        else:
            row.SetValue(field[0], val)
        t = t + 1
    # insert the row into the table
    rows.InsertRow(row)
    ln = ln + 1
    del row
    del t
    del line
del rows

Listing 9: Bringing the text data into a file geodatabase

# import arcgisscripting module
import arcgisscripting
# instantiate gp object
gp = arcgisscripting.create()

# define our function we call to delete objects
def killObject( object ):
    """ Kills an input object """
    if gp.Exists(object):
        gp.Delete_management(object)

# make xy event layer from table
gp.MakeXyEventLayer_management('C:/Testing/NBI.gdb/Test', 'LON', 'LAT', 'tempXyFc', '')
# set our spatial reference
sr = 'GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982]],\
      PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
# if the featureclass already exists, lets get rid of it first
#   actually we have to do this or it will bomb
killObject( 'C:/Testing/NBI.gdb/Bridges/Bridges' )
# copy features from temp layer created via xy event layer to featureclass
gp.CopyFeatures_management('tempXyFc', 'C:/Testing/NBI.gdb/Bridges/Bridges')

Listing 10: Creating a point featureclass from a standalone geodatabase table

# import time module
import time
# open our log file; if it doesn't exist, it gets created
#  here we set it to append ('a') to an existing log file
#  but we can also set to write over an existing one ('w')
log = open('c:/testing/logtest.log','a')
# write out a simple message to the log file - notice how
#   we escape the apostrophe with a backslash
log.write('Here\'s your log file!')
# create datestamp and timestamp based on current local time
datestamp = time.strftime('%m/%d/%Y', time.localtime())
timestamp = time.strftime('%I:%M:%S %p', time.localtime())
# write date/time stamps to log file
log.write('\nThe current date is ' + datestamp)
log.write('\nThe current time is ' + timestamp)
# close the log file
log.close()

Listing 11: Writing to a log file

# import smtplib module
import smtplib
# open our log file, read it into memory
file = open('c:/testing/logtest.log').read()
# print the file object to stdout just to see what it looks like
file
'Message here\nThe current date is 02/26/2008\nThe current time is 06:07:50 PM'
# create a connection to our email server via the IP address
server = smtplib.SMTP('XXX.X.XX.XXX')
# let our message be the contents of the log file
msg = file
# send out the email
server.sendmail('from@me.com','to@you.com', msg)
# terminate the connection to server and quit
server.quit()

Listing 12: Sending log file contents in a email