<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Changing Column Order with Python Export CSV in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18553#M1442</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Currently using this &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/60684-Quick-export-CSV-w-headers-via-python" rel="nofollow" target="_blank"&gt;script&lt;/A&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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)]&amp;nbsp; allRows = [] for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; rowlist = [] &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldnames: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rowlist.append(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; allRows.append(rowlist) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; csvFile.writerow(fieldnames) for row in allRows: &amp;nbsp;&amp;nbsp;&amp;nbsp; csvFile.writerow(row)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can I augment this script to reformat the table?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Visual example (This comes out of SQL so there's no commas):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;pointID Latitude Longitude HydroID UnqHydroID UnqTransID OBJECTID IS_Xing&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 45.35959 -72.30345 337314 337314001 33731400100 1 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 45.35805 -72.30215 337314 337314001 33731400085 2 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;to&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Longitude Latitude HydroID UnqHydroID UnqTransID OBJECTID IS_Xing&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-72.30345 45.35959 337314 337314001 33731400100 1 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-72.30215 45.35805 337314 337314001 33731400085 2 0&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 04 Dec 2013 15:25:23 GMT</pubDate>
    <dc:creator>MatthewSchmitt1</dc:creator>
    <dc:date>2013-12-04T15:25:23Z</dc:date>
    <item>
      <title>Changing Column Order with Python Export CSV</title>
      <link>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18553#M1442</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Currently using this &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/60684-Quick-export-CSV-w-headers-via-python" rel="nofollow" target="_blank"&gt;script&lt;/A&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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)]&amp;nbsp; allRows = [] for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; rowlist = [] &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldnames: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rowlist.append(row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; allRows.append(rowlist) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; csvFile.writerow(fieldnames) for row in allRows: &amp;nbsp;&amp;nbsp;&amp;nbsp; csvFile.writerow(row)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can I augment this script to reformat the table?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Visual example (This comes out of SQL so there's no commas):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;pointID Latitude Longitude HydroID UnqHydroID UnqTransID OBJECTID IS_Xing&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 45.35959 -72.30345 337314 337314001 33731400100 1 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 45.35805 -72.30215 337314 337314001 33731400085 2 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;to&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Longitude Latitude HydroID UnqHydroID UnqTransID OBJECTID IS_Xing&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-72.30345 45.35959 337314 337314001 33731400100 1 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-72.30215 45.35805 337314 337314001 33731400085 2 0&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Dec 2013 15:25:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18553#M1442</guid>
      <dc:creator>MatthewSchmitt1</dc:creator>
      <dc:date>2013-12-04T15:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Column Order with Python Export CSV</title>
      <link>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18554#M1443</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Take a look at the csv.DictWriter class: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://docs.python.org/2/library/csv.html" rel="nofollow" target="_blank"&gt;http://docs.python.org/2/library/csv.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can then specify a dictionary to writerow, with the order specified as a list when the DictWriter was created. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You should then also change your loop to be a list of dictionaries instead of a list of lists, aka a 2D array, as it is now.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not tested, but this should work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, csv fc = "E:\HUC2_DATA\HUC02\NewShapes\Result.gdb\HUC02Transect200m15pt" #input feature class rows = arcpy.SearchCursor(fc) fieldnames = [f.name for f in arcpy.ListFields(fc)] csvFile = csv.DictWriter(open("E:\HUC2_DATA\HUC02\NewShapes\HUC02Test.csv", 'wb'), fieldnames) #output csv&amp;nbsp; allRows = [] for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; rowlist = {} &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldnames: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rowlist['field'] = row.getValue(field)) &amp;nbsp;&amp;nbsp;&amp;nbsp; allRows.append(rowlist) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; csvFile.writeheader() for row in allRows: &amp;nbsp;&amp;nbsp;&amp;nbsp; csvFile.writerow(row)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Dec 2013 15:36:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18554#M1443</guid>
      <dc:creator>JohnReiser</dc:creator>
      <dc:date>2013-12-04T15:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Column Order with Python Export CSV</title>
      <link>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18555#M1444</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks! I believe that's the module I'm looking for. As a new comer to Python though, I can't seem to figure out how to properly format the code to change the fieldnames. Do I separate them at the end of &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;csvFile = csv.DictWriter(open("E:\HUC2_DATA\HUC02\NewShapes\HUC02Test.csv", 'wb'), fieldnames)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;where the 'fieldnames' is? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Another question is does the 2D array free up memory? When I tried running my original script on a shape with 23 million rows it ran out of memory.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Dec 2013 18:30:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18555#M1444</guid>
      <dc:creator>MatthewSchmitt1</dc:creator>
      <dc:date>2013-12-04T18:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Column Order with Python Export CSV</title>
      <link>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18556#M1445</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When you create the csv.DictWriter, the list of fieldnames determines the order in which the fields are written to the .csv file.&amp;nbsp; I would recommend modifying your code to write the rows right after you read them in.&amp;nbsp; With 23 million rows, you don't want to read all of the rows into memory and then write out the data.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you want to keep working on performance, there is a good session from the 2013 DevSummit about geoprocessing in Python and performance.&amp;nbsp; The newer versions of ArcGIS have a new search cursor that runs significantly faster.&amp;nbsp; You may have some other alternative approaches that are even faster, possibly using numPy arrays.&amp;nbsp; Look at the documentation on search cursors.&amp;nbsp; It's usually good to del row, rows when you are done with the search cursor to release any locks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Dec 2013 23:52:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/m-p/18556#M1445</guid>
      <dc:creator>NathanHeick</dc:creator>
      <dc:date>2013-12-04T23:52:04Z</dc:date>
    </item>
  </channel>
</rss>

