Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Changing Column Order with Python Export CSV

$
0
0
If I wanted to change the order of columns and export a shapefiles attribute table to csv is there any way to do that with the following script? (I'm still really new at python sorry)

Currently using this script:

Code:

import arcpy, csv
fc = "E:\HUC2_DATA\HUC02\NewShapes\Result.gdb\HUC02Transect200m15pt" #input feature class
rows = arcpy.SearchCursor(fc)
csvFile = csv.writer(open("E:\HUC2_DATA\HUC02\NewShapes\HUC02Test.csv", 'wb')) #output csv
fieldnames = [f.name for f in arcpy.ListFields(fc)]

allRows = []
for row in rows:
    rowlist = []
    for field in fieldnames:
        rowlist.append(row.getValue(field))
    allRows.append(rowlist)
   
csvFile.writerow(fieldnames)
for row in allRows:
    csvFile.writerow(row)

Can I augment this script to reformat the table?

Visual example (This comes out of SQL so there's no commas):

pointID Latitude Longitude HydroID UnqHydroID UnqTransID OBJECTID IS_Xing
1 45.35959 -72.30345 337314 337314001 33731400100 1 0
2 45.35805 -72.30215 337314 337314001 33731400085 2 0


to

Longitude Latitude HydroID UnqHydroID UnqTransID OBJECTID IS_Xing
-72.30345 45.35959 337314 337314001 33731400100 1 0
-72.30215 45.35805 337314 337314001 33731400085 2 0

Viewing all articles
Browse latest Browse all 2485

Trending Articles