I have made a program that reads a file and its main function does some geographic computations to the contents inside. It works now, but instead I want the program to write it instead to a file instead of printing it. I don't understand how to write it because this are so many and i dont know how to convert all of this into one concatenated string then write it into a file.

def main():

    with open ("LotData1.txt", "r") as file:
        sideList = []
        for i in file:
            temp = i.strip().split()
            sideList.append([temp[0], temp[1], float(temp[2])])



    obj = Lot(sideList, "", "")
    Departures = obj.getDepartureClosure()
    Latitudes = obj.getLatitudeClosure()
    Departures_Correction = obj.getDepartureCorrectedClosure()
    Latitudes_Correction = obj.getLatitudeCorrectedClosure()




    count = len(sideList)

    print "INPUT VALUES:\nCOURSE\tBEARING\t\tDISTANCE\t\tLATITUDE\tCORRECTION\tDEPARTURE\tCORRECTION"
    for i in range(count):
        print "%s \t  %s \t %+6.3f\t%+10.3f\t%+10.3f\t%+10.3f\t%+10.3f"%\
        (sideList[i][0], sideList[i][1], sideList[i][2], obj.getLatitudeClosure()[i], Latitudes_Correction[i], Departures[i], Departures_Correction[i])


    AdjustedLatitudes,  AdjustedDepartures, Adjusted_Distances = obj.adjustByCompassRule()
    AdjustedBearings = obj.adjustBearings()
    print "\n\nADJUSTED VALUES:\nCOURSE\tLATITUDE\tDEPARTURE\tDISTANCE\tBEARING"
    for i in range(count):

        print "%s\t%+10.3f\t%+10.3f\t%+10.3f\t%5d-%d-%d"%\
        (sideList[i][0], AdjustedLatitudes[i], AdjustedDepartures[i], Adjusted_Distances[i],AdjustedBearings[0][i],AdjustedBearings[1][i],AdjustedBearings[2][i])


    Northings = obj.GetNorthings()
    Eastings = obj.GetEastings()
    print "\n\nADJUSTED COORDINATES:\nCORNER\tNORTHING\tEASTING"
    for i in range(count):
        print "%s\t%10.3f\t%10.3f" %\
        (sideList[i][0][0:1],Northings[i-1],Eastings[i-1])


    LEC = obj.getLinearErrorOfClosure()
    TotalDistance = obj.getTotalDistance()
    Departure_Closure = sum(Departures)
    Latitude_Closure = sum(Latitudes)
    RelativePrecision = obj.getRelativePrecision()
    LECBearing = obj.getLECBearingToString()
    CorrLatClosure = obj.getLatCorrectedClosure()
    CorrDepClosure = obj.getDepCorrectedClosure()



    print
    print "OTHER STATISTICS:"
    print " LATITUDE CLOSURE:", Latitude_Closure

    print " DEPARTURE CLOSURE:", Departure_Closure
    print
    print " LINEAR ERROR OF CLOSURE (LEC):", LEC
    print " LEC BEARING: ", LECBearing
    print
    print " TOTAL DISTANCE:", TotalDistance

    print " RELATIVE PRECISION: 1:", RelativePrecision

    print " CORRECTED LATITUDE CLOSURE:", CorrLatClosure
    print " CORRECTED DEPARTURE CLOSURE", CorrDepClosure

Here is a way. I replaced all the print statements by calls to a function printout(). This function is built by using the print_function feature which makes python 2 look like python 3. Note that you can still use print in other parts of the code but with a function syntax.

The from __future__ statement must appear at the very top of the program.

from __future__ import print_function
from functools import partial

def main2(ofile):

    printout = partial(print, file=ofile)

    with open ("LotData1.txt", "r") as file:
        sideList = []
        for i in file:
            temp = i.strip().split()
            sideList.append([temp[0], temp[1], float(temp[2])])



    obj = Lot(sideList, "", "")
    Departures = obj.getDepartureClosure()
    Latitudes = obj.getLatitudeClosure()
    Departures_Correction = obj.getDepartureCorrectedClosure()
    Latitudes_Correction = obj.getLatitudeCorrectedClosure()




    count = len(sideList)

    printout("INPUT VALUES:\nCOURSE\tBEARING\t\tDISTANCE\t\tLATITUDE\tCORRECTION\tDEPARTURE\tCORRECTION")
    for i in range(count):
        printout("%s \t  %s \t %+6.3f\t%+10.3f\t%+10.3f\t%+10.3f\t%+10.3f"%\
        (sideList[i][0], sideList[i][1], sideList[i][2], obj.getLatitudeClosure()[i], Latitudes_Correction[i], Departures[i], Departures_Correction[i]))


    AdjustedLatitudes,  AdjustedDepartures, Adjusted_Distances = obj.adjustByCompassRule()
    AdjustedBearings = obj.adjustBearings()
    printout("\n\nADJUSTED VALUES:\nCOURSE\tLATITUDE\tDEPARTURE\tDISTANCE\tBEARING")
    for i in range(count):

        printout("%s\t%+10.3f\t%+10.3f\t%+10.3f\t%5d-%d-%d"%\
        (sideList[i][0], AdjustedLatitudes[i], AdjustedDepartures[i], Adjusted_Distances[i],AdjustedBearings[0][i],AdjustedBearings[1][i],AdjustedBearings[2][i]))


    Northings = obj.GetNorthings()
    Eastings = obj.GetEastings()
    printout("\n\nADJUSTED COORDINATES:\nCORNER\tNORTHING\tEASTING")
    for i in range(count):
        printout("%s\t%10.3f\t%10.3f" %\
        (sideList[i][0][0:1],Northings[i-1],Eastings[i-1]))


    LEC = obj.getLinearErrorOfClosure()
    TotalDistance = obj.getTotalDistance()
    Departure_Closure = sum(Departures)
    Latitude_Closure = sum(Latitudes)
    RelativePrecision = obj.getRelativePrecision()
    LECBearing = obj.getLECBearingToString()
    CorrLatClosure = obj.getLatCorrectedClosure()
    CorrDepClosure = obj.getDepCorrectedClosure()



    printout()
    printout("OTHER STATISTICS:")
    printout(" LATITUDE CLOSURE:", Latitude_Closure)

    printout(" DEPARTURE CLOSURE:", Departure_Closure)
    printout()
    printout(" LINEAR ERROR OF CLOSURE (LEC):", LEC)
    printout(" LEC BEARING: ", LECBearing)
    printout()
    printout(" TOTAL DISTANCE:", TotalDistance)

    printout(" RELATIVE PRECISION: 1:", RelativePrecision)

    printout(" CORRECTED LATITUDE CLOSURE:", CorrLatClosure)
    printout(" CORRECTED DEPARTURE CLOSURE", CorrDepClosure)

def main():
    with open('outputfile.txt', 'w') as ofile:
        main2(ofile)

Bonus: if you call main2(sys.stdout), the program works like the first version with print!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.