Select by attribute then exporting selection

1859
21
Jump to solution
01-30-2012 05:30 AM
NathanBaylot1
New Contributor
arcpy.TableToTable_conversion("Master_TAHI", "C:\\Users\\D2148\\Documents\\Maps\\Parish Roads\\TAHI_LRS\\Parish_roads\\TAHI_parish\\FIPS", "FIPS001", "\"FIPS PARIS\" = '001'")

I am trying to select by attributes in a .dbf table then export it out to a new .dbf with the selected features only. I am having a problem executing this file. It keeps saying there is a error. The maximum record length has been exceeded. The file only has 87000 records.

Also, once this is figured out. How do I continue to go to the next select by attribute using a while statement. I need it to go to the FIPS PARIS = 127 and each FIPS is odd starting with 001 (eg. 001,003,005...)

If the table to table way doesn't work, any suggestions would be helpful. Thanks.
Tags (2)
0 Kudos
21 Replies
NathanBaylot1
New Contributor
Still having problems with the where clause. The select features will not go through the list loop. It only wants to find the list name since it is in quotes.
I just can't figure this out.
0 Kudos
MathewCoyle
Frequent Contributor
Your code gives me a syntax error so it definitely won't run if that is what you are using.
This should be your where-clause I believe.
"FIPS_PARIS = '" + fip + "'"
0 Kudos
curtvprice
MVP Esteemed Contributor
Still having problems with the where clause. The select features will not go through the list loop. It only wants to find the list name since it is in quotes.
I just can't figure this out.


You've got a syntax error. I highly recommend using a syntax checker. Corrected (using escapes for the single quotes -- just for clarity)
arcpy.TableSelect_analysis(inputTable, outputWorkspace +\
    "\\fips_" + str(fip) + ".dbf", \
    "FIPS_PARIS = " +  "\'" + fip + "\'")

Using string formatting and the os.path module to construct paths is a lot easier to code and debug:
query = "FIPS_PARIS = \'%s\'" % fip
outTable = os.path.join(outputWorkspace,"fips_%s.dbf" % fip)
arcpy.TableSelect_analysis(inputTable,outTable,query)

And there is a new string format() method (introduced in Python 2.4) that will eventually replace it:
query = "FIPS_PARIS = \'{0}\'".format(fip)
outTable = os.path.join(outputWorkspace,"fips_{0}.dbf".format(fip)
arcpy.TableSelect_analysis(inputTable,outTable, query)
0 Kudos
NathanBaylot1
New Contributor
import arcpy
FIPS = "C:\\Users\\D2148\\Documents\\Maps\\FIPS"
c = 0
# FIPSlist = ('001', '003', '005', '007', '009', '011', '013', #Try smaller list first
        #'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')
#FIPSnum = (001, 003, 005, 007, 009, 011, 013, #Try Smaller list first
       # 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)
FIPSlist = ('001', '003', '005')
FIPSnum = (001, 003, 005)
while c < len(FIPSlist):
        for fip in FIPSlist:
                query = "FIPS_PARIS ='" + fip + "'"
               # arcpy.TableToTable_conversion("TRIAL1",FIPS,"FIPS" + FIPSlist + ".dbf",query)
                arcpy.TableSelect_analysis("Master_TAHI", FIPS + "\\FIPS" + FIPSlist + ".dbf",query)
        c = c + 1

I think this will work as it was trying to work and made the first file correct. However, it will not loop anymore
0 Kudos
curtvprice
MVP Esteemed Contributor
However, it will not loop anymore


Your loop is incorrect. You don't need a counter or while loop.

FIPSlist = ('001', '003', '005') for fip in FIPSlist:     query = "FIPS_PARIS ='" + fip + "'"     arcpy.TableSelect_analysis("Master_TAHI", FIPS + r"\FIPS" + fip + ".dbf",query)
0 Kudos
NathanBaylot1
New Contributor
It finally worked! Thank you so much!
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 worked great. The thing is... I tried this combo many times. I think it was the placement of the " or '.
0 Kudos
AmySwan
New Contributor
I'm new to Python and what I want to do is very similar, only my list is of integer values (created by using range()). Using the above example, how would the query expression be changed if FIPSlist was integer instead of string (query field is also integer)?
0 Kudos
curtvprice
MVP Esteemed Contributor
I'm new to Python and what I want to do is very similar, only my list is of integer values (created by using range()). Using the above example, how would the query expression be changed if FIPSlist was integer instead of string (query field is also integer)?


Just do it without the quotes. Also, to use the integer as part of the expression you need to convert the number to a string, using str() too:

FIPSlist = range(5) # (0,1,2,3,4)
for fip in FIPSlist:
    query = "FIPS_PARIS = " + str(fip)
    arcpy.TableSelect_analysis("Master_TAHI", FIPSpath + r"\FIPS" + str(fip) + ".dbf",query)
0 Kudos
ChrisSnyder
Regular Contributor III
How about:

import arcpy
myOutputTable = r"C:\temp\test.gdb\input"
for i in range(1,1001):
   myOutputTable = r"C:\temp\test.gdb\table_id_" + str(i)
   arcpy.TableSelect_analysis(myInputTable, myOutputTable, "MY_ID = " + str(i))
0 Kudos
sofiatavantzi
New Contributor
Hello! i think my question is simple but i can not find a way on answering it.
I want  to make a query in attribute table(arcmap) in which i will get the results for the maximum value of a column.
I have a table with 4 columns [District] [total number of schools] [students] [name of schools], i am interested to get the result from the column students, since the question is 'which district has the most students'.
How can i represend in arcmap the value max?
0 Kudos