Unwanted Spaces and Iterative Loop

Jason Friedman jsf80238 at gmail.com
Sun Jan 26 21:00:35 EST 2014


>
> 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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140126/e65b46b8/attachment.html>


More information about the Python-list mailing list