If you used BRAINscript string functions in LAE, the following examples show how to complete the same task in the Python-based nodes.
You can test the following examples by taking a Create Data node, connecting it to a Transform node, then copy and pasting the examples into the ConfigureFields and ProcessRecords properties of the Transform node.
For more information on each of the following Python functions, please see the Python documentation, for example: https://docs.python.org/2/library/string.html
BRAINscript | Python |
---|---|
|
x[:num] |
|
lstrip(x) |
|
isspace() |
|
{}.format(x) |
|
x.replace() |
|
x[num:] |
|
rstrip(x) |
|
x.split('char') |
|
x.find('char') |
|
len(x) |
|
x[n:m] |
|
x.lower() |
|
x.upper() |
|
x.strip() |
x[:num]
Take two string fields and for the first field, output the first 3 characters, then for the second field, output the first 5 characters. If a record has a value of Null, output this value as Null:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output fields
outputs[0]['left_type'] = unicode
outputs[0]['left_junk'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Select the left substrings while handling Null values
if inputs[0]['type'] is Null:
outputs[0]['left_type'] = Null
else:
outputs[0]['left_type'] = inputs[0]['type'][:3]
if inputs[0]['junk'] is Null:
outputs[0]['left_junk'] = Null
else:
outputs[0]['left_junk'] = inputs[0]['junk'][:5]
lstrip(x)
Take the string values from the color field and strip any whitespace from the left of the strings, then output the trimmed values in a new field called "lstrip_color":
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['lstrip_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Strip whitespace from the left of the strings while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['lstrip_color'] = Null
else:
outputs[0]['lstrip_color'] = inputs[0]['color'].lstrip()
isspace()
Take a string field and check for records that only consist of whitespace. Output Null values as "Null":
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['is_space_junk'] = bool
outputs[0]['is_space_alt_junk'] = bool
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Test if it is whitespace while handling Null values
if inputs[0]['junk'] is Null:
outputs[0]['is_space_junk'] = Null
elif inputs[0]['junk'].strip() == '':
outputs[0]['is_space_junk'] = True
else:
outputs[0]['is_space_junk'] = False
# Alternatively use the built-in isspace() function.
# Note, to be equivalent to the BRAINScript isSpace()
# function you have to override isspace() for cases where
# the value is an empty string
if inputs[0]['junk'] is Null:
outputs[0]['is_space_alt_junk'] = Null
elif inputs[0]['junk'] == '':
outputs[0]['is_space_alt_junk'] = True
else:
outputs[0]['is_space_alt_junk'] = inputs[0]['junk'].isspace()
{}.format(x)
Reformat the values of the color field to use the "*" character to pad the string to the left or right:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output fields
outputs[0]['lpad_color'] = unicode
outputs[0]['rpad_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Pad on the left of the strings (i.e. right-align)
# fill with '*' characters
# while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['lpad_color'] = Null
else:
outputs[0]['lpad_color'] = '{:*>15}'.format(inputs[0]['color'])
# Pad on the right of the strings (i.e. left-align)
# fill with '*' characters
# while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['rpad_color'] = Null
else:
outputs[0]['rpad_color'] = '{:*<15}'.format(inputs[0]['color'])
x.replace()
Output the values from the color field in a new field called "replace_color", replacing all "e" characters with a "*" character:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['replace_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Replace the specified characters in the strings
# while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['replace_color'] = Null
else:
outputs[0]['replace_color'] = inputs[0]['color'].replace('e','*')
x[num:]
Output a substring starting at a specified character e.g. outputs[0]['right_type'] = inputs[0]['type'][3:]
will output the records from the input field "type" in a new field called "right_type", starting from character number 3, where 0 is the first character:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output fields
outputs[0]['right_type'] = unicode
outputs[0]['right_junk'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Select the right substrings while handling Null values
if inputs[0]['type'] is Null:
outputs[0]['right_type'] = Null
else:
outputs[0]['right_type'] = inputs[0]['type'][3:]
if inputs[0]['junk'] is Null:
outputs[0]['right_junk'] = Null
else:
outputs[0]['right_junk'] = inputs[0]['junk'][5:]
rstrip(x)
Take the string values from the color field and strip any whitespace from the right of the strings, then output the trimmed values in a new field called "rstrip_color":
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['rstrip_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Strip whitespace from the right of the strings while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['rstrip_color'] = Null
else:
outputs[0]['rstrip_color'] = inputs[0]['color'].rstrip()
x.split('char')
Take the values of the junk field and split each string into a list of words, using the specified character as the break point. Output the strings as a list in a new field called "split_junk". For example, June 25, 1970
becomes ['June', '25,', '1970']
:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['split_junk'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Split the string into a list using the specified
# characters while handling Null values
if inputs[0]['junk'] is Null:
_result = Null
else:
_result = inputs[0]['junk'].split(' ')
# Convert to a string so we can view the results
# as a list object cannot be directly output
outputs[0]['split_junk'] = unicode(_result)
x.find('char')
Search the values in a field to find a specified character, then output the string location of the character, where the first character of the string is 0 and -1 indicates that the character does not exist in a record:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['index_junk'] = int
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Find the specified character in the strings
# while handling Null values
if inputs[0]['junk'] is Null:
outputs[0]['index_junk'] = Null
else:
outputs[0]['index_junk'] = inputs[0]['junk'].find('o')
len(x)
Output the string length of a specified field:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['len_type'] = int
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Get the length of the strings while handling Null values
if inputs[0]['type'] is Null:
outputs[0]['len_type'] = 0
else:
outputs[0]['len_type'] = len(inputs[0]['type'])
x[n:m]
Take the values of the "type" field, and output a substring of the characters starting and ending at specified string locations, specified in the following format [start:end]
, where end
is up to but not including. For example: string = "Hello"
H | e | l | l | o |
0 | 1 | 2 | 3 | 4 |
[1:4]
would output "ell".
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output fields
outputs[0]['sub_type'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Select the left substrings while handling Null values
if inputs[0]['type'] is Null:
outputs[0]['sub_type'] = Null
else:
outputs[0]['sub_type'] = inputs[0]['type'][2:5]
# Note the second argument is the *position* of the end character
# and is *not* included in the substring.
# In BRAINScript the second argument is the *number* of characters
# to be included in the substring
x.lower()
Convert all upper case characters in a specified field to lower case:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['lower_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Lowercase the strings while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['lower_color'] = Null
else:
outputs[0]['lower_color'] = inputs[0]['color'].lower()
x.upper()
Convert all lower case characters in a specified field to upper case:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['upper_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Uppercase the strings while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['upper_color'] = Null
else:
outputs[0]['upper_color'] = inputs[0]['color'].upper()
x.strip()
Trim the whitespace from the left and right sides of the values in a specified field:
ConfigureFields
#Configure all fields from input 'inputs[0]' to be mapped
#to the corresponding fields on the output 'outputs[0]'
outputs[0] += inputs[0]
# Specify the new output field
outputs[0]['trim_color'] = unicode
ProcessRecords
#Copy all fields from input 'inputs[0]' to the
#corresponding output fields in output 'outputs[0]'.
#Copies the values of the fields that have been setup
#in the mapping defined in the ConfigureFields property
outputs[0] += inputs[0]
# Strip whitespace from the left and right of the strings
# while handling Null values
if inputs[0]['color'] is Null:
outputs[0]['trim_color'] = Null
else:
outputs[0]['trim_color'] = inputs[0]['color'].strip()