PYSTRING( ) function

Returns a character value calculated by a function in an external Python script. Data processing in Python is external to Analytics.

Syntax

PYSTRING("PyFile,PyFunction", length <,field|value <,...n>>)
Name Type Description
PyFile,PyFunction

character

The name of the Python script to run followed by a comma and then the name of the function that returns the value:

 "myScript,myFunction"

When specifying the Python script, omit the file extension. The function you call may call other functions within the script or within other scripts, however all scripts that run must be placed inside a folder in the PYTHONPATH system environment variable prior to running.

For more information, see Configuring Python for use with Analytics.

Note

Your PyFunction must return a Python string object.

length numeric The length to allocate for the return string.
field |value <,...n>

optional

character

numeric

datetime

logical

This list of fields, expressions, or literal values to use as arguments for the Python function. The values are passed into the function you call in the order you specify them.

You may include as many arguments as necessary to satisfy the function definition in the Python script.

Note

Use the ALLTRIM() function to remove any leading or trailing spaces from character input: ALLTRIM(str). For more information, see ALLTRIM( ) function.

Output

Character.

Examples

Basic examples

Returns "my test":

PYSTRING('hello,main', 20, "my")

External Python script that accepts a string and concatenates " test" to the string:

#! python
# hello.py content
def main(str):
    str2 = str + ' test'
    return(str2)

Advanced examples

Returning a substring

This example removes the last two characters from the Vendor Name field and returns the substring:

PYSTRING( "hello,sub_set", LENGTH(Vendor_Name), ALLTRIM(Vendor_Name), LENGTH(ALLTRIM(Vendor_Name)), 0, LENGTH(ALLTRIM(Vendor_Name)) - 2)

External Python script that accepts a string, a string length, and two character positions. The function returns a substring between position one and position two:

#hello.py content
def sub_set(str, length, p1, p2):
    if p1 >= 0 and p2 < length and p1 < p2:
        str2 = str[p1:p2]
    else:
        str2 = str
    return str2