Share

Replace Unicode Values

Definition: replaceUnicode(inputString: str, replacement: str) → str
Description: Replaces instances of Unicode characters in the input string with the replacement string
Parameters: - inputString: The string in which you have to replace the Unicode characters
- replacement: The string you want to replace the Unicode characters with
Returns: A string where all Unicode characters are replaced with the specified replacement string

The following script takes an input string <withUnicode> and a replacement string <”-“>. It replaces the Unicode characters in the input string with the replacement string, and then assigns the resulting value to the <withoutUnicode> field.

Notes:

  • The u prefix before the string indicates that it is a Unicode string.
  • Replace the placeholder values in the script with your own values.
from io.hevo.api import Event
from io.hevo.api import Utils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()

    <withUnicode> = u'<text with Unicode characters>'
    properties['<withUnicode>'] = withUnicode
    properties['withoutUnicode'] = Utils.replaceUnicode(withUnicode, <"-">)
    ...

Sample Transformation

Consider a variable, name with value Westeros King P, where P is the Cyrillic letter represented by Unicode character U0420.

The replaceUnicode function replaces the Unicode character U0420 with a hyphen (-), and assigns the resulting value to the withoutUnicode field.

from io.hevo.api import Event
from io.hevo.api import Utils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()

    properties['namewithoutUnicode'] \
        = Utils.replaceUnicode(properties[name'], '-')

    return event

Sample Output

The value Westeros King P' is changed to Westeros King -, and the modified value is assigned to the new field, withoutUnicode.

Sample Output - Replace Unicode Values


Last updated on Jun 30, 2023

Tell us what went wrong

Skip to the section