# address.py # Reads a list of addresses and formats them nicely for printing. import string import Tkinter, tkFileDialog # Use some graphical widgets to get the Filename # This function inspired by: # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438123 # This function opens a file for reading and returns the file handle def getFilename(): root = Tkinter.Tk() root.withdraw() file = tkFileDialog.askopenfile(parent=root,mode='r',title='Choose a file') return file def main(): print "This program reads a list of addresses and print them formatted" # get the file name #infileName = raw_input("What file are the addresses in? ") # open the file #infile = open(infileName, 'r') # Use a widget to get and open the file infile = getFilename() print # process each line of the input file for line in infile: addr2 = None # get the first and last names from line address = string.split(line, ",") # Split the fields on comma if len(address) == 4: # We do not have a second address line addr1, city, state, zip = address[:] else: # We have two address lines addr1, addr2, city, state, zip = address[:] print "The address is:" print addr1 if addr2 != None: print addr2 print "%s, %s %s" %(city,state,zip) print # close both files infile.close() main()