Hi,
I've Feature Class which keeps changing (e.g. S_pol_Din_DepL82270692015146eAnt015130) and I would like use Arcpy to replace a text (like 2015178eAnt015146) from him field (d2clust200m). Some rows are empty, e.g <Null>, and I don't want change them. But, I can't use replace with a text from ListFeatureClass, it shows that is done, But, isn't.
The text that will be replaced is always the same from the last piece's name from the another FC (it always starts with "pol_din").
If it's too hard be done by arcpy, I can use MB. Or even replace just what isn't null.
Next, I show all tries I've done.
import arcpy
...
... # Local variables:
... arcpy.env.workspace = r'C:\Teste_Auto_CLEIA\Auto_Classi\Dinamica_GDB.gdb'
...
... #List FC # line 6
... FC = arcpy.ListFeatureClasses("S_pol_Din*")
... Feature = FC[0]
... print"Feature: "+str(Feature)
... strFeature = str(Feature)
... print"strFeature: "+strFeature
...
... #List FC # line 13
... FCName = arcpy.ListFeatureClasses("pol*")
... FC_Name = FCName[1]
... print"FC_Name: "+str(FC_Name)
... FCNameCut = FC_Name[-17:]
... print"FCNameCut "+FCNameCut
...
... #Calculate field (D2clust200m) #Line 20
... arcpy.CalculateField_management (Feature, "D2clust200m", "!D2clust200m!.replace('"'FCNameCut'"', \"test\")","PYTHON")
... print"Try 1 Calculated"
...
... #Calculate field (D2clust200m)
... #arcpy.CalculateField_management (Feature, "D2clust200m", "!D2clust200m!.replace(\"FCNameCut\", \"test\")","PYTHON")
... #print"2nd try Calculated"
...
... #Calculate field (D2clust200m)
... #arcpy.CalculateField_management (Feature, "D2clust200m", '!D2clust200m!.replace('"'FCNameCut'"', '"'test'"')',"PYTHON")
... #print"Try 3 Calculated"
...
... #Calculate field (D2clust200m) #This one works if I don't use a list's word
... #arcpy.CalculateField_management (Feature, "D2clust200m", '!D2clust200m!.replace(\"FCNameCut\", \"test\")',"PYTHON")
... #print"4th try Calculated"
... print"Done"
...
Feature: S_pol_Din_DepL82270692015146eAnt015130
strFeature: S_pol_Din_DepL82270692015146eAnt015130
FC_Name: pol_Din_DepL82270692015178eAnt015146
FCNameCut 2015178eAnt015146
Try 1 Calculated
Done
Thanks
*I'm using Arcmap 10.4 with Python 2.7, but I appreciate if it works on ArcMap 10.1
Solved! Go to Solution.
Script works successfully!!!
Thanks Randy and everyone who helps.
import arcpy
...
... # Local variables:
... arcpy.env.workspace = r'C:\Teste_Auto_CLEIA\Auto_Classi\Dinamica_GDB.gdb'
...
... #List FC # Line 6
... FC = arcpy.ListFeatureClasses("S_pol_Din*")
... Feature = FC[0]
... print"Feature: "+str(Feature)
...
... #List Another FC # Line 11
... FCName = arcpy.ListFeatureClasses("pol*")
... FC_Name = FCName[1]
... print"FC_Name: "+str(FC_Name)
... FCNameCut = FC_Name[19:36] # Isn't original line
... print"FCNameCut "+FCNameCut
...
... #Calculate field (D2clust200m) #Line 18
... with arcpy.da.UpdateCursor(Feature, ('D2clust200m')) as curs:
... for row in curs:
... if row[0] is not None:
... row[0] = row[0].replace(FCNameCut, "test")
... curs.updateRow(row)
... print"Randy's try"
Good to hear that you were able to solve it using Randy Burton suggestion. Do keep in mind that a field that with Null values will remain Null, which could be what you want. I'm still not convinced about using the list featureclasses and selecting the first or second value from a list. Could you share a list of the content of two file geodatabases to see hoe the names differ and to see if there may be a more "stable" (less error prone) way of getting the featuresclasses you want?
I have three Feature Classes and all them are at the same geodatabase.
For help you, I send a screen grab that we are talking about.
Good luck.
*Addition info, I know the FeatureClass[1] has the piece's name that I'm looking for. And I don't know how to retrieve the records from the FeatureClass'field to use at the script.
I would start with moving setting to he beginning of the script. Like this:
def main():
import arcpy
# settings
ws = r'C:\Teste_Auto_CLEIA\Auto_Classi\Dinamica_GDB.gdb'
wildcard1, index1 = "S_pol_Din*", 0
wildcard2, index2 = "pol*", 1
fld_name = 'D2clust200m'
name_from, name_to = 19, 36
replace_text = "test"
arcpy.env.workspace = ws
try:
fc1 = arcpy.ListFeatureClasses(wildcard1)[index1]
print ("fc1: {}".format(fc1))
fc2 = arcpy.ListFeatureClasses(wildcard2)[index2]
print ("fc2: {}".format(fc2))
fc_name = fc2[name_from: name_to]
print ("fc_name: {}".format(fc_name))
# update cursor
with arcpy.da.UpdateCursor(fc1, (fld_name)) as curs:
for row in curs:
if row[0] is not None:
row[0] = row[0].replace(fc_name, replace_text)
curs.updateRow(row)
print("Randy's try")
except Exception as e:
print(e)
if __name__ == '__main__':
main()
In case you have multiple geodatabases that require the same treatment, you can create a loop to "walk" through multiple folders and subfolders and apply the process to every fgdb it finds.
Bruno. Try to simplify the problem and what you are trying to do and then build from there.
Try the following code.
import arcpy
fc = r"C:\Teste_Auto_CLEIA\Auto_Classi\Dinamica_GDB.gdb\S_pol_Din_DepL82270692015146eAnt015130"
fieldname = "D2clust200m"
codeblock = "searchtext=\"2015146eAnt015130\""
arcpy.CalculateField_management(fc, fieldname, "!D2clust200m!.replace(searchtext,\"Test\")", "PYTHON_9.3", codeblock)
Interesting path, but I didn't find a way for switch a text (e.g. 2015178eAnt015146) for a result from a ListFeatureClass
I tried, but it doesn't works. I tried similar version, none works.
import arcpy
...
... # Local variables:
... arcpy.env.workspace = r'C:\Teste_Auto_CLEIA\Auto_Classi\Dinamica_GDB.gdb'
...
... #List FC # line 6
... FC = arcpy.ListFeatureClasses("S_pol_Din*")
... Feature = FC[0]
... print"Feature: "+str(Feature)
...
... #List AnotherFC # line 11
... FCName = arcpy.ListFeatureClasses("pol*")
... FC_Name = FCName[1]
... print"FC_Name: "+str(FC_Name)
... FCNameCut = FC_Name[19:36] # Isn't original lane
... print"FCNameCut "+FCNameCut
...
... #Calculate field (D2clust200m) #Line 18
... print("FCNameCut is: {}".format(FCNameCut))
... if FCNameCut is not None:
... expr = FC_Name.replace(str(FCNameCut), "test") #I tried with FCNameCut and not with FC_Name too
... arcpy.CalculateField_management (Feature, "D2clust200m", expr,"PYTHON_9.3")
... print"Done"
...
Feature: S_pol_Din_DepL82270692015146eAnt015130
FC_Name: pol_Din_DepL82270692015178eAnt015146
FCNameCut 2015178eAnt015146
FCNameCut is: 2015178eAnt015146
Runtime error Traceback (most recent call last): File "<string>", line 22, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3129, in CalculateField raise e ExecuteError: ERROR 000539: Error running expression: pol_Din_DepL8227069test Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'pol_Din_DepL8227069test' is not defined Failed to execute (CalculateField).
two suggestions... one a repeat...
1. do the calculate field manually and copy the Results window python snippet
2 show a screen grab of the field and the values in it to make sure we are dealing with plausible entries