Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64798
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: Unwanted Spaces and Iterative Loop |
| Date | 2014-01-26 22:20 +0000 |
| References | <988fec60-228a-4427-b07e-b4327c7e02ae@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6004.1390774848.18130.python-list@python.org> (permalink) |
On 26/01/2014 21:46, matt.s.marotta@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')
> 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")
> 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.
>
> 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!
>
Make your life easier by using the csv module to read and write your
data, the write using the excel-tab dialect, see
http://docs.python.org/3/library/csv.html#module-csv
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unwanted Spaces and Iterative Loop matt.s.marotta@gmail.com - 2014-01-26 13:46 -0800
Re: Unwanted Spaces and Iterative Loop Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-26 22:20 +0000
Re: Unwanted Spaces and Iterative Loop MRAB <python@mrabarnett.plus.com> - 2014-01-26 23:28 +0000
Re: Unwanted Spaces and Iterative Loop Jason Friedman <jsf80238@gmail.com> - 2014-01-26 16:44 -0700
Re: Unwanted Spaces and Iterative Loop matt.s.marotta@gmail.com - 2014-01-26 15:56 -0800
Re: Unwanted Spaces and Iterative Loop Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-27 00:40 +0000
Re: Unwanted Spaces and Iterative Loop matt.s.marotta@gmail.com - 2014-01-26 17:15 -0800
Re: Unwanted Spaces and Iterative Loop Chris Angelico <rosuav@gmail.com> - 2014-01-27 12:56 +1100
Re: Unwanted Spaces and Iterative Loop matt.s.marotta@gmail.com - 2014-01-26 17:58 -0800
Re: Unwanted Spaces and Iterative Loop Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 13:32 +0000
Re: Unwanted Spaces and Iterative Loop Jason Friedman <jsf80238@gmail.com> - 2014-01-26 19:00 -0700
Re: Unwanted Spaces and Iterative Loop matt.s.marotta@gmail.com - 2014-01-26 19:07 -0800
csiph-web