select by attribute then export selection (script tool)

3813
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
1 Solution

Accepted Solutions
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

View solution in original post

0 Kudos
17 Replies
BenjaminGale
New Contributor III
In order to allow a user to input data you'll need to use arcpy.GetParameterAsText(#). This will allow the script to use the information the use inputs in the tool.

For example...
import arcpy
FIPS = arcpy.GetParameterAsText(0)
FIPSlist = arcpy.GetParameterAsTest(1)
SQL = arcpy.GetParameterAsTest(2)
Output = arcpy.GetParameterAsTest(3)

#Rest of code....


The rest of your code should work the same but you will use the variables in place of the 'hard coded' values.

After you have the script done you'll just have to make the script tool.

This page from the help document should help (the other pages from that section may be useful too).

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Adding_a_script_tool/00150000001r00000...

You'll more less just have to tell the tool where the script is and then setup each parameter with a name and the appropriate data type.
The parameters in the tool properties list corrispond directly to the GetParameterAsText values. So GetParameterAsText(0) will be the 1st row of that section, GetParameterAsText(1) the 2nd and so on.

Hope that helps.
0 Kudos
NathanBaylot1
New Contributor
Thanks and that does help. I got the getparameterastext() part. It's essentially value = input() or value = rawinput(). I guess my problem is the creating the tool part. I have read all the help documents on it and can't make heads or tails on some of it. For instance, on the list portion of my code. I don't know what kind of data type it would be. I haven't been coding for long and haven't gotten the grasp of putting it all together yet.
import arcpy
import sys
import string
import os
fcIn = arcpy.GetParameterAsText(0)
fclist = arcpy.GetParameterAsText(1)
sqlclause = arcpy.GetParameterAsText(2)
fcOut = arcpy.GetParameterAsText(3)
for att in fclist:
  arcpy.TableSelect_analysis("fcin", fcin + att + ".dbf",sqlclause)

This is my work in progress. The other question would be. On the sqlclause portion, how do I get it to loop through the list with a different parameter each time. I can do this in hardcoding with a tuple or list. But the tool is different and I can't seem to link them.
0 Kudos
NathanBaylot1
New Contributor
This comes up with a error: name 'env' not defined
import arcpy
import sys
import string
import os
Input = arcpy.GetParameterAsText(0)
Attribute = arcpy.GetParameterAsText(1)
sqlclause = arcpy.GetParameterAsText(2)
env.workspace = arcpy.GetParameterAsText(3)
for att in Attribute:
  arcpy.TableSelect_analysis(Input, Input + att, sqlclause)

[ATTACH=CONFIG]11665[/ATTACH]

[ATTACH=CONFIG]11664[/ATTACH]
0 Kudos
SusanWitherly
New Contributor
You just need to add:
from arcpy import env
0 Kudos
NathanBaylot1
New Contributor
Thanks that got rid of that part. Now it just won't work.
<class 'arcgisscripting.ExecuteError'>: ERROR 000210: Cannot create output C:\Users\D2148\Documents\Maps\FIPS\Master_TAHI0
Failed to execute (TableSelect).
Failed to execute (Selectsplit).
0 Kudos
AlessandroCinnirella
New Contributor III
don't you miss the ".dbf" in the output table??


ciao,
AC
0 Kudos
NathanBaylot1
New Contributor
Yep sure did. As soon as I posted that I knew I missed it. I changed it a few minutes ago and that error went away and brought back a new.
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:
  arcpy.TableSelect_analysis(Input, Input + att + ".dbf", sqlclause)


<type 'exceptions.AttributeError'>: 'module' object has no attribute 'GetParameterAstext'
Failed to execute (Selectsplit).
0 Kudos
AlessandroCinnirella
New Contributor III
is it the wole code? because the error means that you have written GetParameterAstext whith a non capital T.
but the code you posted seems ok...

AC
0 Kudos
NathanBaylot1
New Contributor
[ATTACH=CONFIG]11685[/ATTACH][ATTACH=CONFIG]11684[/ATTACH]
[ATTACH=CONFIG]11687[/ATTACH]
[ATTACH=CONFIG]11688[/ATTACH]
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:
  arcpy.TableSelect_analysis(Input, Input + att + ".dbf", sqlclause)

These are copy and pasted exactly how I ran them
0 Kudos