CSV

1569
9
Jump to solution
04-26-2017 03:25 AM
JohnMcoy
New Contributor III

I am very new to Python programming and have been tasked with writing a program to export a csv file from a file geodatabase feature class. The csv should contain only certain columns with results.

Maybe someone has any suggestions ? Or maybe someone has already done it ? 

Thanks for any help !

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

If it's a one-off task, I wouldn't bother with writing a program for it, but it you need to repeat, then yes.

There are a number of ways that you could do this, but one option is to use

Table To Table—Help | ArcGIS Desktop     to filter our just the fields you need, and save as a .csv

There are code samples at the bottom of the help page that should get you started on what you need.

View solution in original post

9 Replies
RebeccaStrauch__GISP
MVP Emeritus

If it's a one-off task, I wouldn't bother with writing a program for it, but it you need to repeat, then yes.

There are a number of ways that you could do this, but one option is to use

Table To Table—Help | ArcGIS Desktop     to filter our just the fields you need, and save as a .csv

There are code samples at the bottom of the help page that should get you started on what you need.

JohnMcoy
New Contributor III

Thank you Rebecca !  

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

If that worked for you, remember to mark the question as answered so the thread can be closed.

0 Kudos
forestknutsen1
MVP Regular Contributor

Rebecca's suggestion is the right way to go. But just FYI you can also build your csv from scratch if you like (or if the out of the box tool does not have the function you need). The bellow code makes a tsv (tab separated values) report for MTRS.

import arcpy
import os

arcpy.env.overwriteOutput = True

# in feature class or table
in_path = r"xxxxx"
# out tsv file
out_path = r"xxxxx"

# tsv write function - the input is a list of all elements for one row in the tsv
def tsv_write(tsv_row):
    # write each element in list to tsv
    for item in tsv_row:
        tsv.write(item + "\t")
    # end tsv row i.e. new line
    tsv.write("\n")

# create search cursor for input feature class
search_cursor = arcpy.da.SearchCursor(in_path, ["field_1",
                                                "field_2",
                                                "field_3"])
# open/create tsv output file
tsv = open(out_path, "w")

# setup some variables to be used in loop
meridian_last = None
township_last = None
township_list = []
first_row_flag = True
section_list = []

# loop over input feature class table row by row
for row in search_cursor:
    # read in values from row
    meridian = row[0]
    township = row[1]
    section = row[2]

    # if it is the first row of the table set township_last to township
    if first_row_flag:
        township_last = township
        first_row_flag = False

    # if the township is the same as the last township then "roll up" the sections for it, i.e. the same township but a different section
    if township == township_last:
        section_list.append(int(section))
    else:
        # sort the sections so that they are in ascending order
        section_list.sort()
        section_str = ""
        # build a string from section list with commas between
        for sec in section_list:
            section_str = section_str + str(sec) + ", "
        # remove hanging comma and space from sections string
        section_str = section_str[:-2]
        print [township_last, section_str]
        # write township and rolled up section string
        tsv_write([township_last, section_str])
        # restart section list
        section_list = []
        section_list.append(int(section))

    # write headers each time township is in a new meridian
    if meridian != meridian_last:
        print meridian
        tsv_write([meridian])
        print ["Township, Range", "Sections"]
        tsv_write(["Township, Range", "Sections"])

    # reset meridian_last and township_last
    meridian_last = meridian
    township_last = township

# output the last township in the table to the tsv
section_list.sort()
section_str = ""
for sec in section_list:
    section_str = section_str + str(sec) + ", "
section_str = section_str[:-2]
print [township_last, section_str]
tsv_write([township_last, section_str])

# close the tsv
tsv.close()
JohnMcoy
New Contributor III

Could you comment a little bit your code ?   Thanks! 

0 Kudos
DanPatterson_Retired
MVP Emeritus

is this related to your other unclosed thread?

https://community.esri.com/thread/193935-python-in-arcgis-need-help

or is it a new task?

0 Kudos
JohnMcoy
New Contributor III

It`s a new task  

0 Kudos
forestknutsen1
MVP Regular Contributor

Sure, I edited the code post

0 Kudos
by Anonymous User
Not applicable

Justinas,

As others have mentioned, there are various ways to accomplish what you're looking for.  I have done just what you are wanting to do in a few of the tools that I have created. 

You can use the Table To Table—Help | ArcGIS Desktop tool that was already mentioned, use the Delete Field—Data Management toolbox | ArcGIS Desktop tool to get rid of the specific fields, and then the Table To Excel—Help | ArcGIS Desktop tool to export the information out to an excel file.