1

I wrote a simple script that iterates trough selected polygons and cuts the points of other layers, and export shapes and txt to different folders according to the zones defined by the polygons. The script runs without problems when I run it from the Python window in ArcMap 10.6.1, but then I require to translate it into an addin button.

That is when my query does not work, I have looked in many questions and modifying the query according to different suggestions and I always get the result <cannot concatenate 'str' and 'Result' objects>. By the way I searched that message and I do no get any result.

Here my code:

try:
    with arcpy.da.SearchCursor(lyrCompilVid, [fldSectorVid]) as rows:   
        for row in rows:
            #print row[0]
            print type(row[0])
            # Selection polygons
            sectorVD = str(row[0])
            print type(sectorVD)
            print "sector: " + sectorVD                           
            qry = '\"ABRV\" = \'' + str(row[0])  + "\'" + ' AND \"CITY_ABRV\" = \'MXXX\'' # original                        
            print qry
            #WriteLog("query:" + qry, LogFilePath) 
            #outDir = os.path.join(parentDir, 'secteur', str(row[0]))  
            outDir = str(parentDir) + '\\secteur\\' + str(row[0])
            print outDir
            sectorName =  str(row[0]).replace("-", "_")
            try:
                # Creation de layer du secteur selectionne:
                sectorLyr = arcpy.SelectLayerByAttribute_management(lyrCompilVid, "NEW_SELECTION", qry)
                print "Terminee la selection de polygone du secteur " + sectorName

            except Exception as err:
                print ("Erreur pendant la creation du layer de secteur: " + sectorName)
                print str(err)

            arcpy.SelectLayerByLocation_management("Fuites_lyr", "INTERSECT", sectorLyr, "", "NEW_SELECTION")           
            selectNrFuites = int(arcpy.GetCount_management('Fuites_lyr').getOutput(0))
            
            print "Le nombre de fuites intersectent le secteur '" + sectorLyr + "' est: " + str(selectNrFuites)

I cut the code there because it is when stops executing, as my print statements tell me. Code updated with more lines. I checked the type of the elements of the query and all are strings. I really hope someone would help me please. The error does not come out with a code, is just a message that shows up in the execution window:

Terminee la selection de polygone du secteur MX_L
cannot concatenate 'str' and 'Result' objects
Processus termine a: 13:12:23.599000
Temps ecoule: 0:00:01.180818

2
  • I can't see a way for the last line of code you include to generate the error, so it makes me think it is a line or two later that is generating the error. Also, paste the error with traceback.
    – bixb0012
    Commented Nov 23, 2022 at 17:49
  • Hi bixb0012, I just updated my question.
    – nanunga
    Commented Nov 23, 2022 at 18:27

1 Answer 1

2

Your error is probably coming from either this code, or something similar:

sectorLyr = arcpy.SelectLayerByAttribute_management(lyrCompilVid, "NEW_SELECTION", qry)
.....
print "Le nombre de fuites intersectent le secteur '" + sectorLyr + "' est: " + str(selectNrFuites)

The error message is saying you can't "add" (+) a string, the first part of your print, with a Result object (sectorLyr). You could attempt to force the result into a string by doing str(sectorLyr), which may or may not work. But I doubt that will return the number of selected features, which is what I think you're after. Most Result objects are created equal in what properties they have. But they won't all work in the same way because tools generate different outputs. I'd read a little more about how the result object works before going much further. Using the .getOutput with the Select tool will probably return the layer, not the number of selected features. If you really need this, run GetCount.

3
  • THANKS THANKS THANKS That was it, but I do not understand is why it was not printing the print statements after the select statement and before that particular print statement you mention, because I used those prints as a way of knowing where my code bugs, but this changes this. Your help has been I could say vital, literally. I have been with that bug three nights already.
    – nanunga
    Commented Nov 23, 2022 at 19:51
  • I should add that I just used str(sectorLyr) as you said.
    – nanunga
    Commented Nov 23, 2022 at 19:54
  • 1
    I think you got that far before failing, as that's the first instance where you take the result object variable and try to print it. Prior to that, it looks to be printing other variables (ordinary things you can print)
    – KHibma
    Commented Nov 23, 2022 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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