Unwanted Spaces and Iterative Loop

MRAB python at mrabarnett.plus.com
Sun Jan 26 18:28:37 EST 2014


On 2014-01-26 21:46, matt.s.marotta at gmail.com wrote:
> I have been working on a python script that separates mailing addresses into different components.
>
> Here is my code:
>
> inFile = "directory"
> outFile = "directory"
> inHandler = open(inFile, 'r')
> outHandler = open(outFile, 'w')

Shouldn't you be writing a '\n' at the end of the line?

> outHandler.write("FarmID\tAddress\tStreetNum\tStreetName\tSufType\tDir\tCity\tProvince\tPostalCode")
> for line in inHandler:

This is being done on every single line of the file:

>      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")
>      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")
>      str = str.replace(" ON","ON\t")
>
>
>      outHandler.write(str)
> inHandler.close()
>
> The text file that this manipulates has 91 addresses, so I'll just paste 5 of them in here to get the idea:
>
> 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
>
> My issue is that in the output file, there is a space before each city and each postal code that I do not want there.
>
You could try splitting on '\t', stripping the leading and trailing
whitespace on each part, and then joining them together again with
'\t'. (Make sure that you also write the '\n' at the end of line.)

> Furthermore, the FarmID is being added on to the end of the postal code under the original address column for each address.  This also is not supposed to be happening, and I am having trouble designing an iterative loop to remove/prevent that from happening.
>
> Any help is greatly appreciated!
>
As Mark said, you could also use the CSV module.




More information about the Python-list mailing list