Question

I would like to extract a string of characters between two underscores. The number of characters between and on each side of the '_' will vary, but there will only ever be two underscores. The long field with underscores is a text field, the field to be filled in is a short integer. I've been able to parse out the characters before and after the underscores and fill other fields in the feature class, but have been unable to place the middle section into a new field.

Example 1: 102_1204_234324

I want to return '1204'

Example 2: 324423_1_342

I want to return '1'

I've tried a number of variations and the one that I think should work is:

# Import system modules
import arcpy
#from arcpy import env

# Set environment settings
arcpy.env.workspace = "c:/temp/testing.gdb"

# Set local variables
inFeatures = "testFeature"
fieldName = "testField"
expression = "!parse_field!.split('_')[1::2]"

# Execute CalculateField 
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON", "")

Which I would think would create a list, then return every second element of the list. However the field to be filled in (testField) is still empty.

Thanks -al

Was it helpful?

Solution

    print "my_test_string".split('_')[1]

Will output "test"

OTHER TIPS

I believe you want a regex:

import re
m = re.search('_.+_', 'goodbye_cruel_world')
m.group(0) # returns '_cruel_'

But wait, we can do better! The following regex uses 'look(ahead|behind)s' to discard the underscores, and so leaves you with what's important: (?<=_).+(?=_)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top