0

I am creating a python script where I am trying to iterate over point feature class. I want to create seperate Feature classes from each individual day. So select daynumber and then export it to a new FC with an unique daynumber as name.

I was unable to find my answer online. So I decided to ask my question here.

ArcGIS 10.2

My script looks as follows:

test1 = "D:\\Mijn_Doc\\Test_Geodatabase.gd\\test1"
dayno = 2618
with test1 as rows:
    for row in rows:
        query = '"daynumber = '+ str(dayno) + '"'
        arcpy.Select_analysis(test1, 'D:\\Mijn_Doc\\Test_Geodatabase.gdb\\select'+str(dayno), query)
    dayno +=1

Gives the following error:

arcgisscripting.ExecuteError: ERROR 000358: Invalid expression "daynumber = 2618" Failed to execute (Select)

The selection looks fine, If I build it in Model Builder and export it to a Python script the expression looks the same as it looks in this Error message.

  • 4
    A lot of your code looks wrong. For example, the extension to the geodatabase for your test1 variable should be .gdb, not .gd. You then appear to want to be performing a search curosor, as you are currently attempting a with statement on a string. This will throw an error. I'd look further into cursors. There should be plenty of examples here and on esri documentation resources.arcgis.com/en/help/main/10.1/index.html#//… – Emil Brundage Jan 12 '15 at 23:52
  • 1
    I agree with Emil. See the code sample on that page which includes with arcpy.da.SearchCursor(fc, fields) as cursor: to get started. – Stephen Lead Jan 12 '15 at 23:59
  • 1
    Rather than exporting a model to Python script, as a means to try and get syntax right, I think it is always better to run tools from their dialog and then go to Geoprocessing | Results to Copy As Python Snippet. – PolyGeo Jan 13 '15 at 1:35
  • 1
    That's a good technique @PolyGeo, that way you only get working code! – Michael Stimson Jan 13 '15 at 1:49
0

Why the cursor?

Just something simple like this will work and be clean:

arcpy.env.workspace = r'C:\ArcGIS\Default.gdb'
for dayno in xrange(2618, 2630, 1):
    arcpy.Select_analysis(test1, "{0}_{1}".format(selecttest, dayno), """daynumber = '{0}'""".format(dayno))
  • Beautiful JGP! I removed the single quotes in the select expression: """daynumber = {0}""" and it run smoothly. – Martijn Jan 17 '15 at 11:31
1

ERROR 000358 is 'the expression is not valid', this doesn't help much. I think though it's similar to Runtime error : ERROR 000358: Invalid expression using SelectLayerByAttribute in ArcPy? where the user was attempting to select a string field populated with numbers without quoting the number.

Your SQL query should be

"daynumber = '"+ str(dayno) + "'"

which when unquoted becomes:

daynumber = '2618'

The field name doesn't need to be quoted or bracketed like in 'select by attributes'; the only time that I've found the field name needs to get special quotes or brackets is in field calculator.

I'm not sure about with test1 as rows:, surely that should be with arcpy.da.SearchCursor(test1) as rows: but it's confusing what you're trying to do in the snippet. You're iterating through rows but then performing a select against the data...

0

For the SQL, you have a double quote in the wrong spot.

Try:

query = '"daynumber" = '+ str(dayno)

0

Thanks for the help everybody.

I followed some of your advises and in the end this was the (working) script I was looking for:

dayno = 2618
with arcpy.da.SearchCursor(test1, ('daynumber',)) as cursor:
for row in cursor:
    query = 'daynumber=%s' % dayno
    if dayno > 2630:
        break
    else:
        arcpy.Select_analysis(test1, selecttest + str(dayno), query)
        dayno +=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.