Update Cursor string error and attribute error

1050
4
Jump to solution
09-18-2023 11:48 AM
RyShissler_Welland
Emerging Contributor

I'm new to scripting and I'm trying to create a tool that takes street address from one field, makes it into a internal link, and then adds it to another field. I keep running into an error on line 69. I can't seem to find a solution.

 

File "<string>", line 69, in execute
AttributeError: __enter__

 

 

 

 

def addressParser(address):
    try:
       splitAddress = address.split(' ' , 1)
       firstLetter = splitAddress[1][0]
       streetName = splitAddress[1]
    except:
        arcpy.AddMessage('An error occurred while updating ' + address + '.')
    return firstLetter , streetName

import arcpy


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = "toolbox"

        # List of tool classes associated with this toolbox
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Water Service Linker"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        param0 = arcpy.Parameter( # Allows user to select the table on which they'd like the action performed
            displayName = 'Table',
            name = 'param0',
            datatype = 'GPTableView',
            parameterType = 'Required',
            direction = 'Input')
        
        params = [param0]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """This tool will add folder link data to the WtrSrvcInstall Field"""

        table = parameters[0].valueAsText # converts the parameter to a useable string
        arcpy.AddMessage(table)
        fields = ['Civic_Name_Label' , 'WtrSrvcInstall'] #Defines the fields we'll be working in
        addressField = 'Civic_Name_Label' # The field title that contains the address info
        #linkField = 'WtrSrvcInstall' # The field title that will be the destination for the links
        folderBase = '\\\\polaris\\Infrastructure_ScanFiles\\WaterServiceSheets' # Sets the base link under which all other links exist
        arcpy.AddMessage(folderBase)
        
        #try:
        with arcpy.UpdateCursor(table , fields) as cursor: # sets the cursor in the correct table and fields
            for row in cursor: # iterates through each row
                first , street = addressParser(row[0])
                folderFull = folderBase + '\\' + first + '\\' + street
                arcpy.AddMessage(folderFull)
                row[1] = folderFull
        #except:
            #arcpy.AddMessage('An error occurred')
                    


        return
        

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Okay, so I'd still not use valueAsText here.

But. 

Try using arcpy.da.UpdateCursor instead; it didn't throw any errors for me.

Oh wait that's probably the issue; arcpy.UpdateCursor and arcpy.da.UpdateCursor have different parameters.

View solution in original post

4 Replies
AlfredBaldenweck
MVP Regular Contributor

Try changing your table variable from parameters[0].valueAsText to parameters[0].value?

You're already asking for a table in the parameter, but you're feeding the cursor a string. Might be something else, but I'd at least give it a shot.

0 Kudos
RyShissler_Welland
Emerging Contributor

Hmmm I got the same error with .value unfortunately. 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Okay, so I'd still not use valueAsText here.

But. 

Try using arcpy.da.UpdateCursor instead; it didn't throw any errors for me.

Oh wait that's probably the issue; arcpy.UpdateCursor and arcpy.da.UpdateCursor have different parameters.

RyShissler_Welland
Emerging Contributor
OMG yes, that worked! Thank you!
0 Kudos