AnsweredAssumed Answered

Python String Format using a placeholder

Question asked by NatiNati1186 on Mar 24, 2020
Latest reply on Mar 25, 2020 by NatiNati1186
import arcpy

arcpy.env.workspace = r"D:\APRX_MXDS\USA_Parcels_2019_Project\Test.gdb"

List_dissolve_fc = arcpy.ListFeatureClasses("*Dissolve")  # get a list of feature classes that end with "Dissolve"

fields = ["CO_NO", "COUNTY_NAME"]  # List of fields in test.gdb

for fc in List_dissolve_fc:  # Loop through each feature class in the list
    with arcpy.da.UpdateCursor(fc, fields) as cursor: # Loop through each feature
        for row in cursor:
            row[1] = "{[-17]}".format(fc) # Update each feature in field [1] "COUNTY_NAME" using the name of the feature class without the last 17 characters. For example: walton_2019pin_Dissolve would be walton.
            cursor.updateRow(row)

Hello,

 

I need to loop through feature classes and in each feature class I need to update the field "COUNTY_NAME" with the name of the feature class. The problem is, that the name of the feature class is too long and I don't need to include the last 17 characters. 

For example: "walton_2019pin_Dissolve" is the name of one feature class and I only want to use "walton" in the field names. 

As you can see in the script this is written in line 12.

This method is not working.

 

How could I do this?

 

Thank you,

Natalia

Outcomes