Need a sample script that queries an attribute and outputs result

820
9
Jump to solution
07-13-2018 04:15 PM
by Anonymous User
Not applicable

I'm new to Python and need a sample ArcPy script which would allow me to query a FC attribute in a .gdb and output the result to my C drive. My FC is name 'Approaches' and the attribute I need to query on is 'Approved' and the value in that attribute I want to pull is the "Yes" value. I would like to export that list of Approved Approaches to my C drive. I would also like to print the Name attribute value of those Approved Approaches. 

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

A basic approach would look something like:

import arcpy

fc = r"C:\Path\To\database.gdb\Approaches" # feature class
fn = r"C:\Path\To\output.txt" # save file name

f = open(fn, 'w')

fields = ['Name', 'Approved'] # can add other fields such as Approved, if needed

whereClause = "Approved = 'Yes'"  # may need to modify, based on field type, how Y-N is stored

with arcpy.da.SearchCursor(fc, fields, where_clause=whereClause) as cursor:
    for row in cursor:
        f.write("{}\t{}".format(row[0], row[1])) # Name and Approved - to file
        print "{}\t{}".format(row[0], row[1]) # Name and Approved - to console if needed

f.close()
print "Done"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

(It will probably need some editing to work with your data.)

View solution in original post

9 Replies
RandyBurton
MVP Alum

A basic approach would look something like:

import arcpy

fc = r"C:\Path\To\database.gdb\Approaches" # feature class
fn = r"C:\Path\To\output.txt" # save file name

f = open(fn, 'w')

fields = ['Name', 'Approved'] # can add other fields such as Approved, if needed

whereClause = "Approved = 'Yes'"  # may need to modify, based on field type, how Y-N is stored

with arcpy.da.SearchCursor(fc, fields, where_clause=whereClause) as cursor:
    for row in cursor:
        f.write("{}\t{}".format(row[0], row[1])) # Name and Approved - to file
        print "{}\t{}".format(row[0], row[1]) # Name and Approved - to console if needed

f.close()
print "Done"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

(It will probably need some editing to work with your data.)

by Anonymous User
Not applicable

I should have time this week or next to give this a try. I appreciate your help on this.

0 Kudos
by Anonymous User
Not applicable

One last thing to I forgot to mention was how could I modify that script to only pull approved approaches within a set time frame?

0 Kudos
RandyBurton
MVP Alum

Add it to your where clause. The SQL syntax may vary slightly depending on the type of database you are using.  Are you using a file geodatabase or another type?

whereClause = "Approved = 'Yes' AND DateField BETWEEN '2018-01-01' AND '2018-07-01'"

whereClause = "Approved = 'Yes' AND DateField > '2018-01-01' AND DateField < '2018-07-01'"
0 Kudos
by Anonymous User
Not applicable

I modified the where clause to pull the current date but it is not successful. I'm not sure what i'm doing wrong but the script runs fine without the where clause date added.

import arcpy, datetime
arcpy.env.overwriteOutput = True

fc = r"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\New File Geodatabase.gdb\Approaches" # feature class
fn = r"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\output.txt" # save file name
today = datetime.date.today()
dte = today.strftime('%m/%d/%Y')

f = open(fn, 'w')

fields = ['Name', 'Approved'] # can add other fields such as Approved, if needed

whereClause = "Approved = 'Yes' AND Date_Approved = {}".format(dte)  # may need to modify, based on field type, how Y-N is stored

with arcpy.da.SearchCursor(fc, fields, where_clause=whereClause) as cursor:
    for row in cursor:
        f.write("{}\t{}".format(row[0], row[1])) # Name and Approved - to file
        print "{}\t{}".format(row[0], row[1]) # Name and Approved - to console if needed

f.close()
print "Done"
0 Kudos
RandyBurton
MVP Alum

In the where clause, try >= with the date, otherwise you might be matching today's date at midnight.  And you might try a different format for the date.

whereClause = "Approved = 'Yes' AND Date_Approved >= '{} 00:00:00'".format(today.strftime('%Y-%m-%d'))

# whereClause =
# "Approved = 'Yes' AND Date_Approved >= '2018-07-18 00:00:00'"‍‍‍‍‍‍‍‍‍‍

Also, can you confirm that the data in Approved is a text string consisting of either 'Yes' or 'No'?  Could it be using some other value?  'Y' or 'N', 'True' or 'False'?

0 Kudos
by Anonymous User
Not applicable

Approved is a string. I was able to correct the script with a couple changes. Below is the finished script and again thanks for all your help!

import arcpy, datetime
arcpy.env.overwriteOutput = True

points = r"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\New File Geodatabase.gdb\Approaches" # feature class
outpath = r"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\New File Geodatabase.gdb" # location of .gdb to create new export fc
today = datetime.date.today() # creating date time variable
dte = today.strftime('%m/%d/%Y') # converts date time variable to string
dtep = today.strftime('%m_%d_%Y') # takes date time string variable and replaces / with underscores to use in export fc name

fields = ['Name', 'Approved', 'Date_Approved'] # define fields for search cursor and print format to use

arcpy.MakeFeatureLayer_management(points, 'Approaches') #makes approaches fc a feature layer for use in this script

arcpy.SelectLayerByAttribute_management('Approaches', "NEW_SELECTION", "Approved = 'Yes' AND Date_Approved = CURRENT_DATE") # creates a selection of approved approaches from the current date
arcpy.FeatureClassToFeatureClass_conversion('Approaches', outpath, 'Approved_Approaches_{}'.format(dtep)) # exports the selection to the outpath variable location

with arcpy.da.SearchCursor(points, fields, "Approved = 'Yes' AND Date_Approved = CURRENT_DATE") as cursor: 
    for row in cursor:
        print "{}\t{}\t{}".format(row[0], " Approved", row[2]) 
print " "
print "Export complete!"
                    
0 Kudos
WinstonHoyle
New Contributor II

Python can be a bit of a learning curve for beginners. The previous script should help but take a look at the documentation so you can understand what the script is doing.

arcpy.da.SearchCursor(fc, fields, where_clause=whereClause)

Documentation: http://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm

An earlier knowledge article about how to learn Python and ArcPy: https://community.esri.com/groups/technical-support/blog/2014/03/26/7-easy-ways-learning-python-arcp...

by Anonymous User
Not applicable

I'm learning slowly in between other projects and YouTube has been a big help. Thanks for the reply!

0 Kudos