relationalai.std.strings.split_part()
split_part(string: Producer, separator: str|Producer, index: int|Producer) -> Expression
Extracts a substring from a string that is split by a specified separator at a given index. If string
or separator
is a Producer
, then split_part()
acts as a filter and removes non-string values from the producer. Must be called in a rule or query context.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
string | Producer or Python str object | The string to split. |
separator | Producer or Python str object | The separator to split the string by. |
index | Producer or Python int object | The index of the substring to extract. |
Returns
Section titled “Returns”An Expression
object.
Example
Section titled “Example”Use split_part()
to extract a substring at a specified index based on a separator:
import relationalai as raifrom relationalai.std import strings
# =====# SETUP# =====
model = rai.Model("MyModel")Person = model.Type("Person")
with model.rule(): Person.add(id=1).set(full_name="Alice Smith") Person.add(id=2).set(full_name="Bob Jones") Person.add(id=3).set(full_name=-1) # Non-string full_name
# =======# EXAMPLE# =======
# Set 'first_name' and 'last_name' properties by extracting parts of the 'full_name' property.with model.rule(): person = Person() person.set( first_name=strings.split_part(person.full_name, " ", 0), last_name=strings.split_part(person.full_name, " ", 1) )
# Since split_part() filters out non-string values, the first_name and last_name# properties are not set for the person with id=3.with model.query() as select: person = Person() response = select(person.id, person.first_name, person.last_name)
print(response.results)# id first_name last_name# 0 1 Alice Smith# 1 2 Bob Jones# 2 3 NaN NaN