return characters between two underscores
Asked Answered
S

2

7

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

Swift answered 25/2, 2014 at 0:22 Comment(0)
B
7
    print "my_test_string".split('_')[1]

Will output "test"

Burnt answered 25/2, 2014 at 0:40 Comment(2)
This was the simplest answer to my question and it worked. Thanks @ksun!Swift
fyi, this actually does not return characters between two underscores, but every character after one underscore (which is what the OP wants, but is not the answer to the question title)Immerge
I
2

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: (?<=_).+(?=_)

Ilyse answered 25/2, 2014 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.