1

I am trying to pass the results of a list as a parameter in a SelectLayerByAttribute expression to output and excel spreadsheet of the attribute table. Here is the code, the results create an empty excel spreadsheet. I am little lost.

# Make feature layer
   arcpy.MakeFeatureLayer_management(OwnerList, "OwnerTable")
   with arcpy.da.SearchCursor("OwnerTable", "ClubName") as cursor:
        myList = sorted({str(row[0]) for row in cursor})

        # Create Club lists
        for i in myList:
            expression = '"ClubName" = \'str(i)\' '
            arcpy.SelectLayerByAttribute_management("OwnerTable", "NEW_SELECTION", expression)
            arcpy.TableToExcel_conversion("OwnerTable", out_xls + "_" + i + ".xls")

Solution: # Make feature layer arcpy.MakeFeatureLayer_management(OwnerList, "OwnerTable") with arcpy.da.SearchCursor("OwnerTable", "ClubName") as cursor: myList = sorted({str(row[0]) for row in cursor}) print myList

    # Create Club lists
    for i in myList:
        expression = ' "ClubName" = \'%s\' ' % i
        print expression
        print arcpy.AddMessage(expression)
        arcpy.SelectLayerByAttribute_management("OwnerTable", "NEW_SELECTION", expression )
        arcpy.TableToExcel_conversion("OwnerTable", out_xls + "_" + i + ".xls")

closed as off-topic by PolyGeo Sep 24 '16 at 5:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Try using arcpy.AddMessage(expression) before Select Layer by Attribute to make sure it's what you expect. – cndnflyr Sep 13 '16 at 15:18
  • cndnflyr - the results from the AddMessage returns a value of "None" for each item in the list. – Ahilgendorf Sep 13 '16 at 15:32
  • Keep moving back up the stream of code to see where the break is. Next check myList with arcpy.AddMessage(myList.ToString()). You might not need to designate ToString()... I always find that somewhere I make an assumption as to what I expect is happening and what is actually happening. It's a lot faster if you work in these checks as you write the code, and keep verifying your results as you go. Once you have it working then all these checks can be discarded. – cndnflyr Sep 13 '16 at 15:40
  • I had a print myList in the code that returns ['3LKS', 'BOBOEN', 'CCC', 'ESD', 'FR', 'FRC', 'HODAG', 'LAC', 'NEWTOM', 'NTU', 'NWP', 'SC', 'VC', 'WIL'] which are the correct (expected) results. I just need to pass these into the expression one at a time. Right now I get the spreadsheets created and formatted properly just no data. – Ahilgendorf Sep 13 '16 at 19:34
  • 1
    Well done! I'm glad you worked it out. Write it as an answer to your own question instead of in these comments. – cndnflyr Sep 16 '16 at 18:47