Unwanted Spaces and Iterative Loop

matt.s.marotta at gmail.com matt.s.marotta at gmail.com
Sun Jan 26 22:07:31 EST 2014


On Sunday, 26 January 2014 21:00:35 UTC-5, Jason Friedman  wrote:
> 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()

Unfortunately this did not work - the columns get messed up and there is no column for the full address.



More information about the Python-list mailing list