Copying only text from a string field to a new field

1319
14
07-19-2012 02:22 AM
PieterSnyders
New Contributor
Good day,

I know this is probably a stupid question. I am working with a river data set and the labeling field is a string field containing text (river names) and integer (river codes). I want to copy only the text (river names) to a new field. How can I do this with the field calculator... any help will be appreciated.

Regards,

P
Tags (2)
0 Kudos
14 Replies
JakeSkinner
Esri Esteemed Contributor
Are the text and integer values separated by a space?  You can most likely use the .split command to do this.  However, this will be a little more difficult if any of the river names contain spaces.  Are all the integer values the same length (i.e. 100, 220, 354, etc)?  It may be best to provide a screen shot of how the field looks in your attribute table.
0 Kudos
PieterSnyders
New Contributor
I have attached two sections of the field
0 Kudos
PieterSnyders
New Contributor
Some of the river names do have spaces
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how you can do this:

Codeblock:
def update(field):
  if field[0] in str(range(0, 9)) or field[0] == '#':
    pass
  else:
    return field


Field=
update(!TAG!)


Be sure 'Python' is checked on at the top of the field calculator.  Here is a screen shot.
[ATTACH=CONFIG]16236[/ATTACH]
0 Kudos
PieterSnyders
New Contributor
Thank you, that worked partially, this is what is left:
0 Kudos
KimOllivier
Occasional Contributor III
Use a  Regular Expression to match names with letters to make a selection, then just do a copy on selected records. Regular expressions are supported in Python and are a very powerful text manipulation tool.

Maybe we could assume that no names start with a digit, so you could reverse the search to look for any integer
But if you didn't, an expression  re.match('[A-Za-z ]+',inField) would allow any combination of letters and spaces

# pseudocode (not run) for interactive Python window
# NOT field calculator (keep that for modelbuilder....)
import arcpy
import re
arcpy.env.workspace = "your_gdb_path"
cur = arcpy.UpdateCursor("table1")
for row in cur:
    m = re.match('[0-9]+',row.inField) # matches any value beginning with integer string of digits
    if not m : # ie not nothing found
        row.nameCopy = m.string
        cur.updateRow(row) # must not forget this or doesn't write to file
del cur # close cursor

# nameCopy is target field, inField is source field
0 Kudos
PieterSnyders
New Contributor
Use a  Regular Expression to match names with letters to make a selection, then just do a copy on selected records. Regular expressions are supported in Python and are a very powerful text manipulation tool.

Maybe we could assume that no names start with a digit, so you could reverse the search to look for any integer
But if you didn't, an expression  re.match('[A-Za-z ]+',inField) would allow any combination of letters and spaces

# pseudocode (not run) for interactive Python window
# NOT field calculator (keep that for modelbuilder....)
import arcpy
import re
arcpy.env.workspace = "your_gdb_path"
cur = arcpy.UpdateCursor("table1")
for row in cur:
    m = re.match('[0-9]+',row.inField) # matches any value beginning with integer string of digits
    if not m : # ie not nothing found
        row.nameCopy = m.string
        cur.updateRow(row) # must not forget this or doesn't write to file
del cur # close cursor

# nameCopy is target field, inField is source field


Where should this code go... I am very new to this and quite confused lol
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Thank you, that worked partially, this is what is left:


Can you send a screen shot of what you entered in the Field Calculator.  It looks like the code did the opposite of what you are looking to accomplish.  It calculated all the text values that begin with a number and '#' sign, and not the values that begin with a character.
0 Kudos
PieterSnyders
New Contributor
Can you send a screen shot of what you entered in the Field Calculator.  It looks like the code did the opposite of what you are looking to accomplish.  It calculated all the text values that begin with a number and '#' sign, and not the values that begin with a character.


Here is the snapshot
0 Kudos