replace characters label ArcMap

899
7
Jump to solution
11-01-2021 05:14 AM
TomasLooström
New Contributor II
Hey!
I wish to use replace twice in the same field.
Attributes: >0980>Visy> GA: 8 must be labeled GA:8.
I use def FindLabel ([externid]): return [externid] .replace ('0980>Visy', ''), but fails to add code so that even the last > disappear.
Anyone know how to write it?
Best regards,
Tomas
Tags (1)
0 Kudos
2 Solutions

Accepted Solutions
by Anonymous User
Not applicable

Without getting to regex, you can chain them with what you need.

 

 

.replace('>0980>Visy>', '').replace('>0980>Fran>', '')

 

 

  If there are a lot, you can create a loop to iterate through all the variations from a list and replace them. Assuming there is a space there between the last > and GA:8,or you can find another way to parse out the part you want to replace.

 

 

for str in [str[0].split(' ')[0] for str in arcpy.da.SearchCursor(yourfc, [externid])]:
   [exernid].replace(str, '')

 

 

or index it as Jayanta provided.

 

[externid].split('>')[-1]

 

 

View solution in original post

by Anonymous User
Not applicable

Is there some that contains spaces (> 0980> Visy>) and others that do not (>0980>Visy) in your field?  If that is the case, I'd suggest using

[externid].split('>')[-1].strip()

 

View solution in original post

7 Replies
by Anonymous User
Not applicable

replace replaces the whatever string you put in the first parameter so you need to be explicit with what you want replaced.

 

.replace('>0980>Visy>', '')

 

 

0 Kudos
TomasLooström
New Contributor II
Thanks for the reply!

I missed writing that Visy can consist of different names, it varies and can then be Fran instead of Visy.

0 Kudos
by Anonymous User
Not applicable

Without getting to regex, you can chain them with what you need.

 

 

.replace('>0980>Visy>', '').replace('>0980>Fran>', '')

 

 

  If there are a lot, you can create a loop to iterate through all the variations from a list and replace them. Assuming there is a space there between the last > and GA:8,or you can find another way to parse out the part you want to replace.

 

 

for str in [str[0].split(' ')[0] for str in arcpy.da.SearchCursor(yourfc, [externid])]:
   [exernid].replace(str, '')

 

 

or index it as Jayanta provided.

 

[externid].split('>')[-1]

 

 

TomasLooström
New Contributor II

When I use .replace ('> 0980> Visy>', '') .replace ('> 0980> Fran>', '') then> 0980>Visy is included in the labeling but last> disappears. 

0 Kudos
by Anonymous User
Not applicable

Is there some that contains spaces (> 0980> Visy>) and others that do not (>0980>Visy) in your field?  If that is the case, I'd suggest using

[externid].split('>')[-1].strip()

 

TomasLooström
New Contributor II

Thanks, it works!

 

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

Check if a similar script works

Parser: Python

(!FieldName!.split(' ',1)[1]).replace(' ','')


Think Location