Select to view content in your preferred language

Table Exports to CSV file

857
4
05-20-2024 10:56 AM
KRobinson-GIS
New Contributor

Hello All,

I was given a seemingly simple task at work to produce a .txt file with a set of data displayed in ascending order of a specific field of a feature class.

I quickly found that I was not able to export a table from Pro to display the data in the order that I wanted. Every time I exported, the resulting table would be in ascending order of the OBJECT ID field, I was never able to export the table in ascending order of the field of my choosing.

I did manage a work around in that I exported the data as a CSV file, opened it in Excel, deleted the column that contained the OBJECT ID field, then saved the file as a .txt file, opened it in Notepad ++, and with my target field now being the first column, I sorted the data in ascending order using the tool in Notepad ++.

I was just curious if anyone has come across the same issue and knows of a way to export a table in ascending order of a field other than the OBJECT ID field, straight from Pro?

Thanks for any insights!

0 Kudos
4 Replies
BlakeTerhune
MVP Regular Contributor

You can use Python to open a search cursor using the fields in the order you want with a sql_clause parameter to sort on the field you want. Iterate through each row of the cursor and write the data to your text file. You can even use the Python CSV module.

CodyPatterson
Frequent Contributor

Hey @KRobinson-GIS 

I'm away from my work machine currently and am not able to test my theory entirely, but I have an idea!

Is it possible for you to swap the column locations to the desired ascending/descending column instead of the OBJECTID column? Here is what I mean:

swap.gif

Hope that helps!

Cody

RhettZufelt
MVP Notable Contributor

From what I can tell, They will always be exported by sort order of OBJECTID.  You can use the Sort GP tool (or python) to sort the table on a field that you want (which creates a new table/FC), then export that new, exported table as the OBJECTID's will be relative to the new 'sorted' field.

If nothing else works, you can do the workaround without the step saving to .txt file.

In Excel, open csv, sort by the field you want, save.

R_

HabtamneshGoite
Occasional Contributor

You can do it all in pro. when Exporting a table of the feature class, it has options to choose fields you want to include as well as sorting. 

HabtamneshGoite_0-1716266866299.png