AnsweredAssumed Answered

Data in CSV file overwritten

Question asked by laurablackburn on Feb 5, 2015
Latest reply on Feb 6, 2015 by laurablackburn

I am exporting my data from multiple Network Anayst VRPs to a CSV file.  I am able to get the data exported to the csv file, but it keeps putting all the data in one row and overwriting it.  I need to be able to advance down to the next row, but I am new to python and not sure how to do this.  any help is greatly appreciated.  Here's the code that writes to the CSV file.

# create table - fields: From script [Block (shortBlockName), Scale (scale)], From Routes layer [stops (OrderCount), dTime (TotalTravelTime), dDistance (TotalDistance)]
        CSVFile = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\analysis2\\allRoutes.csv'
        f = open (CSVFile, "wb")
        w = csv.writer(f)
        fieldNames = ['OrderCount', 'TotalTravelTime', 'TotalDistance', 'Block', 'Scale']
        w.writerow(fieldNames)
        recordList = []
        # print "Data type: " + desc.dataElement.dataType # dataType = layer

        # examine children and print their name and datatype - no children
        lyrFile = arcpy.mapping.Layer(outLayerFile)
        for lyr in arcpy.mapping.ListLayers(lyrFile):
            if lyr.name == "Routes":
                #print "Routes location: " + lyr.datasetName # returns 'Routes'
                #print "Routes long Name: " + lyr.longName   # returns 'TrapRoute_IA1_1K\Routes'
                desc = arcpy.Describe(lyr)
                # indexFields = 0
                # for field in desc.fields:
                #    print "%-22s %s %s" % (field.name, ":", field.type) # this returns all of the fields in the routes table
                cntCursor = arcpy.da.SearchCursor(lyr, "OrderCount")
                for row in cntCursor:
                    cnt = row[0]
                    recordList.append(str(cnt))
                    print ("OrderCount: {}".format(cnt))
                timeCursor = arcpy.da.SearchCursor(lyr, "TotalTravelTime")
                for row in timeCursor:
                    t = row[0]
                    recordList.append(str(t))
                    print ("TravelTime: {}".format(t))
                distCursor = arcpy.da.SearchCursor(lyr,"TotalDistance")
                for row in distCursor:
                    dist = row[0]
                    recordList.append(str(dist))
                    print ("TravelDist: {}".format(dist))
                recordList.append(str(shortBlockName))
                recordList.append(str(scale))
                print ("Records: {}".format(recordList))    # returns:  [102, 144.19780824705958, 61.627489665697794, 'IA1', '1000']
                print ("Block: {}".format(shortBlockName))  # returns: IA1
                print ("Scale: {}".format(scale))           # returns: 1000
        w.writerow(recordList)
        w.next() # returns error - I need a way to move to the next row
    f.close()
# create table - fields: From script [Block (shortBlockName), Scale (scale)], From Routes layer [(OrderCount), (TotalTravelTime), (TotalDistance)]        CSVFile = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\analysis2\\allRoutes.csv'        f = open (CSVFile, "wb")        w = csv.writer(f)        fieldNames = ['OrderCount', 'TotalTravelTime', 'TotalDistance', 'Block', 'Scale']        w.writerow(fieldNames)        recordList = []        # print "Data type: " + desc.dataElement.dataType # dataType = layer        # examine children and print their name and datatype - no children        lyrFile = arcpy.mapping.Layer(outLayerFile)        for lyr in arcpy.mapping.ListLayers(lyrFile):            if lyr.name == "Routes":                #print "Routes location: " + lyr.datasetName # returns 'Routes'                #print "Routes long Name: " + lyr.longName  # returns 'TrapRoute_IA1_1K\Routes'                desc = arcpy.Describe(lyr)                # indexFields = 0                # for field in desc.fields:                #    print "%-22s %s %s" % (field.name, ":", field.type) # this returns all of the fields in the routes table                cntCursor = arcpy.da.SearchCursor(lyr, "OrderCount")                for row in cntCursor:                    cnt = row[0]                    recordList.append(str(cnt))                    print ("OrderCount: {}".format(cnt))                timeCursor = arcpy.da.SearchCursor(lyr, "TotalTravelTime")                for row in timeCursor:                    t = row[0]                    recordList.append(str(t))                    print ("TravelTime: {}".format(t))                distCursor = arcpy.da.SearchCursor(lyr,"TotalDistance")                for row in distCursor:                    dist = row[0]                    recordList.append(str(dist))                    print ("TravelDist: {}".format(dist))                recordList.append(str(shortBlockName))                recordList.append(str(scale))                print ("Records: {}".format(recordList))    # returns:  [102, 144.19780824705958, 61.627489665697794, 'IA1', '1000']                print ("Block: {}".format(shortBlockName))  # returns: IA1                print ("Scale: {}".format(scale))          # returns: 1000        w.writerow(recordList)        w.next() # returns error - I need a way to move to the next row    f.close() 

Outcomes