Replacing record from a field with a List

562
17
Jump to solution
02-18-2018 05:27 PM
BrunoDeus1
New Contributor III

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

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

If you want to use the update cursor as Xander Bakker is suggesting, you will need to add a test so you can skip fields with null values:

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)

View solution in original post

17 Replies
RandyBurton
MVP Regular Contributor

For your field calculator line, do you get better results if you try:

# No quotes (") around FCNameCut 
arcpy.CalculateField_management (Feature, "D2clust200m", '!D2clust200m!.replace(FCNameCut, \"test\")',"PYTHON_9.3")

May also want to use "PYTHON_9.3".

BrunoDeus1
New Contributor III

Randy, It didn't works

#Calculate field (D2clust200m) #This one works if I don't use a list's word                             # Line 20
... arcpy.CalculateField_management (Feature, "D2clust200m", '!D2clust200m!.replace(FCNameCut, \"test\")',"PYTHON_9.3")
... print"Randy 1 try Calculated"
... print"Done"
...
Feature: S_pol_Din_DepL82270692015146eAnt015130
strFeature: S_pol_Din_DepL82270692015146eAnt015130
FC_Name: pol_Din_DepL82270692015178eAnt015146
FCNameCut 2015178eAnt015146
Runtime error Traceback (most recent call last): File "<string>", line 21, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 3360, in CalculateField raise e ExecuteError: ERROR 000539: Error running expression: u"2015178eAnt015146".replace(FCNameCut, "test") Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'FCNameCut' is not defined Failed to execute (CalculateField).

Is there some idea?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

are you still using this line in your code?

FCNameCut = FC_Name[-17:]

maybe you should show the full cleanedup code prior to trying Randy's suggestion

BrunoDeus1
New Contributor III

Yes, I was using the line. I changed this line, accepted the Randy's tip and cleaned the script. I show the last version:

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 FC                                                # line 11
... FCName = arcpy.ListFeatureClasses("pol*")
... FC_Name = FCName[1]
... print"FC_Name: "+str(FC_Name)
... FCNameCut = FC_Name[19:36]   # I changed now
... print"FCNameCut "+FCNameCut
...
... #Calculate field (D2clust200m)            #Line 18
... arcpy.CalculateField_management (Feature, "D2clust200m", '!D2clust200m!.replace(FCNameCut, \"test\")',"PYTHON_9.3")
... print"Randy 1 try Calculated"
... print"Done"
...
Feature: S_pol_Din_DepL82270692015146eAnt015130
FC_Name: pol_Din_DepL82270692015178eAnt015146
FCNameCut 2015178eAnt015146
Runtime error Traceback (most recent call last): File "<string>", line 19, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 3360, in CalculateField raise e ExecuteError: ERROR 000539: Error running expression: u"2015178eAnt015146".replace(FCNameCut, "test") Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'FCNameCut' is not defined Failed to execute (CalculateField).

Something else to do?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I think replicating the field name in the expression could be an issue.  If this doesn't work, then copy the code snippet from the Results window to get the correct syntax.  Do this for a few selected records that do not contain <null>

print("FCNameCut is: {}".format(FCNameCut))
if FCNameCut is not None:
    expr = FC_Name.replace(str(FCNameCut), "test")‍
    arcpy.CalculateField_management (Feature, "D2clust200m", expr,"PYTHON_9.3")
0 Kudos
XanderBakker
Esri Esteemed Contributor

You have to keep in mind that when you use an expression like:

'!D2clust200m!.replace(FCNameCut, \"test\")'

... it will use FCNameCut as a string and not its content. You need to have the content and include the quotes to make sure it is interpreted correctly. The expression may require additional quotes due to the double interpretation (python and calculate field parser).

You can also use a cursor to update the values:

with arcpy.da.UpdateCursor(Feature, ('D2clust200m')) as curs:
    for row in curs:
        row[0] = row[0].replace(FCNameCut, "test")
        curs.updateRow(row)

You also have me pretty confused by the way you get the "Feature" and "FC_Name".  Listing the featureclasses and get the first item from the first list and second items from the second lists. You don't know how the featureclasses are named? Does each fgdb that you will use this code on have the same content? 

BrunoDeus
New Contributor II

Every week the file's name chenges. The last part of name came's from a data. Both Features Classes at the script are diferente (name and content).

You are right that was confusing about Features Classes, so I explained it better at line 11

I tried your suggest too

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

... with arcpy.da.UpdateCursor(Feature, ('D2clust200m')) as curs:

... for row in curs:

... row[0] = row[0].replace(FCNameCut, "test")

... curs.updateRow(row)

... print"Bakker try"

...

Feature: S_pol_Din_DepL82270692015146eAnt015130

FC_Name: pol_Din_DepL82270692015178eAnt015146

FCNameCut 2015178eAnt015146

Runtime error

Traceback (most recent call last):

File "<string>", line 21, in <module>

AttributeError: 'NoneType' object has no attribute 'replace'

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

NoneType' object has not attribute 'replace' 

means this line

row[0] = row[0].......

row[0 is None

RandyBurton
MVP Regular Contributor

If you want to use the update cursor as Xander Bakker is suggesting, you will need to add a test so you can skip fields with null values:

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)

View solution in original post