Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64817

Re: Unwanted Spaces and Iterative Loop

References <988fec60-228a-4427-b07e-b4327c7e02ae@googlegroups.com> <52e5aafa$0$29999$c3e8da3$5496439d@news.astraweb.com> <0503362d-7f14-441f-9291-75711eddd283@googlegroups.com>
Date 2014-01-26 19:00 -0700
Subject Re: Unwanted Spaces and Iterative Loop
From Jason Friedman <jsf80238@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6015.1390788044.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

>
> I`m not reading and writing to the same file, I just changed the actual
> paths to directory.
>
> This is for a school assignment, and we haven`t been taught any of the
> stuff you`re talking about.  Although I appreciate your help, everything
> needs to stay as is and I just need to create the loop to get rid of the
> farmID from the end of the postal codes.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

If you are allowed to use if/then this seems to work:

inFile = "data"
outFile = "processed"
inHandler = open(inFile, 'r')
outHandler = open(outFile, 'w')
for line in inHandler:
    if line.startswith("FarmID"):

outHandler.write("FarmID\tAddress\tStreetNum\tStreetName\tSufType\tDir\tCity\tProvince\tPostalCode\n")
    else:
        line = line.replace(" ","\t", 1)
        line = line.replace(" Rd,","\tRd\t\t")
        line = line.replace(" Rd","\tRd\t")
        line = line.replace("Ave,","\tAve\t\t")
        line = line.replace("Ave ","\tAve\t\t")
        line = line.replace("St ","\tSt\t\t")
        line = line.replace("St,","\tSt\t\t")
        line = line.replace("Dr,","\tDr\t\t")
        line = line.replace("Lane,","\tLane\t\t")
        line = line.replace("Pky,","\tPky\t\t")
        line = line.replace(" Sq,","\tSq\t\t")
        line = line.replace(" Pl,","\tPl\t\t")

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

        outHandler.write(line)
inHandler.close()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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