Sie sind auf Seite 1von 3

# Author: Ali Qureshi

# Course: GISC9317 "Advanced Programming for GIS"


# Deliverable: Manipulate Street Address
# Date: February 13 2018
# Purpose: To separate a full mailing address into different address components,
# such as street number, street name, street suffix type, street direction, city,
province, and postal code, etc.
# by using Python scripting and displaying the output results in a text file.
# Note: This python script will look for the input file called
d1RawListofFarms.text in the C:\temp\D1RawData folder. It will create a file
called QureshiAListofFarms in the C:\temp\d1Results folder.

#----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------

# Import the output file into the following Directory. Setting up connection with
the following directory.
import os
if os.path.exists (r'C:\temp\d1Result'):
import shutil
shutil.rmtree (r'C:\temp\d1Result')

# If the following directory does not exists that create it.


if not os.path.exists (r'C:\temp\d1Result'):
os.makedirs (r'C:\temp\d1Result')

# Use the following as the input file path and name.


inputFile = r'C:\temp\d1RawData\d1RawListOfFarms.txt'
# Use the following as the path and name of the output file.
outputFile = r'C:\temp\d1Result\QureshiAListofFarms.txt'

# set up inFileHandler to open the input file and read it.


inFileHandler = open(inputFile, 'r')
# set up outFileHandler to open the output file and write on it.
outFileHandler = open(outputFile, 'w')
# define the variable for direction.
dirlist = ['S', 'N', 'W', 'E']

# Create a variable to display the column header in the first row.


fieldNames = 1
# Create a count for the streetNameField variable in order to combine spaces
between the two names of the street.
count = 1

for line in inFileHandler:


# Set the first row to include headers FarmID.Adress etc. all separated by a tab.

if fieldNames == 1:
# Define variable for the header row.
titleRow =
'FarmID\tAddress\tStreetNum\tStreetName\tSufType\tDir\tCity\tProvince\tPostalCode\n
'
outFileHandler.write(titleRow)

#Defining variables and splitting strings for all 9 column in the output file based
on tabs, lines and spaces.
else:
#Define removeN to start data on second line.
removeN = line.rstrip('\n')
#Define farm ID as first value in row and than include a tab.
farmID = removeN.split ('\t')[0]
#Define address as second value in row and than include a tab.
address = removeN.split ('\t') [1]
#Define splitAddress as first value from "address" delimited by a comma and space.
splitAddress = address.split (', ') [0]
#Define city as second value from "address" delimited by a comma and space.
city = address.split (', ') [1]
#Define Province as third value from "address" delimited by a comma and space.
Province = address.split (', ') [2]
#Define splitAddress0 as second word delimited by space from "splitAddress".
splitAddress0 = splitAddress.split (' ')
#Define streetNumField as the first string from "splitAdress0".
streetNumField = splitAddress0 [0]
#Define streetNameField as the second and third string from "splitAddress0".
streetNameField = splitAddress0[1:-1]

#If statement indicating, if there is a direction value in "splitAddress0"


#than define streetDirField as the string behind "splitAddress0" and define
StreetSufField as the string
#two spots behind "SplitAddress0".

if splitAddress0 [-1] in dirlist:


streetDirField = splitAddress0 [-1]
streetSufField = splitAddress0 [-2]
streetNameField = splitAddress0 [1:-2]
#If the count value is less than the Street Name field than include a space between
the first and second word.
# of streetNameField.
if count < len (streetNameField):
streetName = streetNameField[0] + ' ' + streetNameField[1]
#Otherwise leave the street name as is with just one word.

else:
streetName = streetNameField[0]

#If there is no direction then define streetSufField as one string behind the end
of
#of "splitAddress0" and define streetNameField as ranging from the two words behind
"splitSufField".
else:
streetDirField = ' '
streetSufField = splitAddress0[-1]
streetNameField = splitAddress0 [1:-1]
#If the count value is less than the Street Name field than include a space between
the first and second word.

if count < len (streetNameField):


streetName = streetNameField[0] + ' ' + streetNameField[1]
#Otherwise leave the street name as is with just one word.
else:
streetName = streetNameField[0]

# Define provincefield as the first word of the province variable delimited by a


space.
provinceField = Province.split (' ')[0]
# Define postalcodefield as the the remaining text following "provinceField"
delimited by a space.
postalcodeField = Province.strip(provinceField + ' ')
#Defining the variables for output with a tab between each field.
finalOutput = (farmID + '\t' + address + '\t' + streetNumField + '\t' +
streetName + '\t' + streetSufField + '\t' + streetDirField + '\t' + city + '\t' +
provinceField + '\t' + postalcodeField + '\n')
#Write the following output on the defined file path.
outFileHandler.write(finalOutput)
#Defining the positon for output writing.
fieldNames = fieldNames + 1

#Closing both input and output files.


inFileHandler.close()
outFileHandler.close()

Das könnte Ihnen auch gefallen