Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'output': 0.05; 'column': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'language.': 0.14; "'w')": 0.16; 'appreciated!': 0.16; 'components.': 0.16; 'csv': 0.16; 'idea:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:o 100': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'file,': 0.19; 'header:User-Agent:1': 0.23; 'skip:l 30': 0.24; 'script': 0.25; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'easier': 0.31; 'file': 0.32; 'there.': 0.32; 'supposed': 0.32; 'text': 0.33; 'url:python': 0.33; 'addresses': 0.33; 'trouble': 0.34; 'skip:s 30': 0.35; 'there': 0.35; 'addresses,': 0.36; 'data,': 0.36; "i'll": 0.36; 'url:org': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'address.': 0.39; 'mailing': 0.39; 'received:org': 0.40; 'space': 0.40; 'read': 0.60; 'url:3': 0.61; 'address': 0.63; 'email addr:gmail.com': 0.63; 'our': 0.64; 'different': 0.65; 'life': 0.66; 'city': 0.66; 'here': 0.66; 'postal': 0.74; 'hunter': 0.84; 'hutchinson': 0.84; 'iterative': 0.84; 'stone': 0.84; 'subject:Unwanted': 0.84; 'rd,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Unwanted Spaces and Iterative Loop Date: Sun, 26 Jan 2014 22:20:36 +0000 References: <988fec60-228a-4427-b07e-b4327c7e02ae@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-146-9-46.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: <988fec60-228a-4427-b07e-b4327c7e02ae@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390774848 news.xs4all.nl 2905 [2001:888:2000:d::a6]:37119 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64798 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