Remove unwanted characters from column

matt.s.marotta at gmail.com matt.s.marotta at gmail.com
Sun Jan 26 22:49:01 EST 2014


School assignment is to create a tab separated output with the original given addresses in one column and then the addresses split into other columns (ex, columns for city, postal code, street suffix).

Here is my code:

inHandler = open(inFile, 'r')
outHandler = open(outFile, 'w')
outHandler.write("FarmID\tAddress\tStreetNum\tStreetName\tSufType\tDir\tCity\tProvince\tPostalCode")
for line in inHandler:
    str = line.replace("FarmID\tAddress", " ")
    outHandler.write(str[0:-1])

    str = str.replace(" ","\t", 1)
    str = str.replace(" Rd, ","\tRd\t\t")
    str = str.replace("Rd ","\tRd\t\t")
    str = str.replace("Ave, ","\tAve\t\t")
    str = str.replace("Ave ","\tAve\t\t")
    str = str.replace("St ","\tSt\t\t")
    str = str.replace("St, ","\tSt\t\t")    
    str = str.replace("Dr, ","\tDr\t\t")
    str = str.replace("Lane, ","\tLane\t\t")
    str = str.replace("Pky, ","\tPky\t\t")
    str = str.replace(" Sq, ","\tSq\t\t")
    str = str.replace(" Pl, ","\tPl\t\t")

    str = str.replace("\tE, ","E\t")
    str = str.replace("\tN, ","N\t")
    str = str.replace("\tS, ","S\t")
    str = str.replace("\tW, ","W\t")
    str = str.replace(",\t","\t\t")
    str = str.replace(", ON ","\tON\t")

    outHandler.write(str)

inHandler.close()
outHandler.close()


Here is some sample addresses, there are over 100:

FarmID	Address
1	1067 Niagara Stone Rd, Niagara-On-The-Lake, ON L0S 1J0
2	4260 Mountainview Rd, Lincoln, ON L0R 1B2
3	25 Hunter Rd, Grimsby, ON L3M 4A3
4	1091 Hutchinson Rd, Haldimand, ON N0A 1K0
5	5172 Green Lane Rd, Lincoln, ON L0R 1B3
6	500 Glenridge Ave, St. Catharines, ON L2S 3A1
7	471 Foss Rd, Pelham, ON L0S 1C0
8	758 Niagara Stone Rd, Niagara-On-The-Lake, ON L0S 1J0
9	3836 Main St, Lincoln, ON L0R 1S0



I have everything worked out, except that the final output places the farmID at the end of postal code as seen in the example below (notice the brackets showing where the farmID is placed):

FarmID	Address	StreetNum	StreetName	SufType	Dir	City	Province	PostalCode 	
1	1067 Niagara Stone Rd, Niagara-On-The-Lake, ON L0S 1J0(1)	1067	Niagara Stone	Rd		Niagara-On-The-Lake	ON	L0S 1J0

Any ideas on how to fix this? Keep in mind as well that the farmID will have 2 characters at a certain point.



More information about the Python-list mailing list