select by attribute then export selection (script tool)

3533
17
Jump to solution
02-02-2012 04:02 AM
NathanBaylot1
New Contributor
This is sort of a continuation of my previous post. http://forums.arcgis.com/threads/48862-Select-by-attribute-then-exporting-selection
I would like to create a script tool that does the same thing but with user defined parameters.
import arcpy FIPS = "C:\\Users\\D2148\\Documents\\Maps\\FIPS" FIPSlist = ('001', '003', '005', '007', '009', '011', '013',          '015', '017', '019', '021', '023', '025', '027',         '029', '031', '033', '035', '037', '039', '041',         '043', '045', '047', '049', '051', '053', '055',         '057', '059', '061', '063', '065', '067', '069',         '071', '073', '075', '077', '079', '081', '083',         '085', '087', '089', '091', '093', '095', '097',         '099', '101', '103', '105', '107', '109', '111',         '113', '115', '117', '119', '121', '123', '125',         '127') for fip in FIPSlist:   query = "FIPS_PARIS ='" + fip + "'"   arcpy.TableSelect_analysis("Master_TAHI", FIPS + "\\FIPS" + fip + ".dbf",query)


This is the original script that works, I just need it to take user defined parameters and I have never created a tool.

- It needs an input dbase, feature class or shapefile (eg. "Master_TAHI")
- A list of attributes the tool needs to loop through (eg. fips = ('001', '003', '005')
- The sql clause (eg. "FIPS_PARIS ='" + fip + "'")
- output with name of attribute at end. (eg. "\\FIPS" + fip + ".dbf")
Tags (2)
0 Kudos
17 Replies
AlessandroCinnirella
New Contributor III
the code seems ok....

check the source of the script-tool and/or save it with a new name and reload it.
0 Kudos
NathanBaylot1
New Contributor
Ok, I saved it under a new name and that seemed to have fixed that. Now, I can see how it is messing up with this error.
[ATTACH=CONFIG]11690[/ATTACH]
When I do a SQL expression like "FIPS_PARIS" = att or Attribute (calling from the previous parameter list) it literally looks for att or Attribute not 001 or 003
0 Kudos
AlessandroCinnirella
New Contributor III
because the sqlclause is defined by the sqlclause = arcpy.GetParameterAsText(2) and it is always the same.
you use the att only to chancge the output table name.

you should modify the scipt in this way
import arcpy
import sys
import string
import os
from arcpy import env
Input = arcpy.GetParameterAsText(0)
Attribute = arcpy.GetParameterAsText(1)
#sqlclause = arcpy.GetParameterAsText(2)  <--- YOU DONT NEED TO SPECIFY THIS PARAMETER
env.workspace = arcpy.GetParameterAsText(2) <---- CHANGE THIS PARAMETER INDEX ACCORDINGLY WITH OTHERS
for att in Attribute:
  sqlclause = "\"FIPS_PARIS\" = '" + att + "'"
  arcpy.TableSelect_analysis(Input, Input + att + ".dbf", sqlclause)



hope this helps,
AC
0 Kudos
NathanBaylot1
New Contributor
Yeah I understand that. I am making a tool and want it to have user defined field and attributes. Not just attributes.
0 Kudos
NathanBaylot1
New Contributor
and different expressions other than =
0 Kudos
AlessandroCinnirella
New Contributor III
ok,

import arcpy import sys import string import os from arcpy import env Input = arcpy.GetParameterAsText(0) Attribute = arcpy.GetParameterAsText(1) sqlclause = arcpy.GetParameterAsText(2)   env.workspace = arcpy.GetParameterAsText(3)  for att in Attribute:   sqlclause = sqlclause + "'" + att + "'"   arcpy.TableSelect_analysis(Input, Input + att + ".dbf", sqlclause) 



but your sql input parameters should not contain attributes. should be like ("FIPS_PARIS" 😃 or ("CHOSEN_FIELD" CHOSEN_OPERATOR)

AC
0 Kudos
NathanBaylot1
New Contributor
Yep your awesome!!!
0 Kudos
NathanBaylot1
New Contributor
import arcpy
import sys
import string
import os
from arcpy import env
Input = arcpy.GetParameterAsText(0)
Attribute = arcpy.GetParameterAsText(1)
Attributelist = Attribute.split(";")
sqlclause = arcpy.GetParameterAsText(2)
env.workspace = arcpy.GetParameterAsText(3)
for att in Attributelist:
  query = sqlclause + "'" + att + "'" 
  arcpy.TableSelect_analysis(Input, Input + att + ".dbf", query)

Final Code
0 Kudos