3

I'm using ArcPy with ArcMap to try and calculate a field called URL. I'm having issues getting the correct syntax when trying to add text and then have a break in the text to add in a field !WellID!, then continue the text. This code will fill the field but is not populating the !WellID! field in there it's just the plain text.

arcpy.CalculateField_management("TX_WW_Final2021","URL", "\"https://www3.twdb.texas.gov/apps/waterdatainteractive//GetReports.aspx?Num=(!WellID!)&Type=SDR-Well\""

This is before: enter image description here

This is after it's run: enter image description here

The text is correct except I need the corresponding number from the !WellID! field to be in the middle of the text URL

0

2 Answers 2

3

I think Field Calculator syntax in python is complicated. It is better to learn and use da.UpdateCursor instead, with format:

import arcpy

feature_class = r'C:\GIS\ArcMap_default_folder\Default.gdb\jl_riks_Intersect_Intersect'
fields = ['OBJECTID', 'URL']

with arcpy.da.UpdateCursor(feature_class, fields) as cursor:
    for row in cursor:
        #row[0] is the first fields value in fields list, in my example OBJECTID
        #row[1] the second, to be calculated below
        newvalue = 'https://www3.twdb.texas.gov/apps/waterdatainteractive//GetReports.aspx?Num=({0})&Type=SDR-Well'.format(row[0])
        row[1] = newvalue #assign the new value to row[1]
        cursor.updateRow(row) #and update it

enter image description here

0
1

The screenshots indicate which product you are using, but it is always good to state the product (ArcGIS Desktop/ArcMap, ArcGIS Pro) and version. This is especially important with Python-related questions because ArcGIS Desktop/ArcMap uses the now-retired Python 2.x while ArcGIS Pro uses Python 3.x.

The reason the code is not working is that !WellID! isn't being expanded to the actual value, it is being inserted as literal text. There are multiple ways to do string formatting in Python, more in Python 3.x now than what existed in Python 2.x at the end. For Python 2.x, I suggest using the string format method with ArcGIS Field Calculator. From 5. Built-in Types -- Python 2.7.18 documentation:

str.format(*args, **kwargs)

Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

When using with Field Calculator, it would look like:

arcpy.CalculateField_management(
    "TX_WW_Final2021",
    "URL",
    "https://www3.twdb.texas.gov/apps/waterdatainteractive//GetReports.aspx?Num=({})&Type=SDR-Well".format(!WellID!)
)

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.