Working with Strings in Python - Python has a lot of useful features built into its data types, as we’ve seen with our record dictionaries, but strings (text values) can do a lot for us as well.
Part 1 - The Default Transform & Working with Records
Part 2 - Reading from Records
Part 3 - Writing to Records
Part 4 - Removing Fields from a Record
Part 5 - Working with Strings in Python
Python has a lot of useful features built into its data types, as we’ve seen with our record
dictionaries, but strings (text values) can do a lot for us as well - Without writing any code, we can easily change the casing of a value:
account_type = "Legacy user"
account_type.lower() # "legacy user"
account_type.upper() # "LEGACY USER"
account_type.title() # "Legacy User"
account_type.swapcase() # "lEGACY USER"
It’s also very easy (and safe) to convert other Python objects (such as numbers) into strings, to take advantage of the other methods that Python strings provide:
account_number = 123 # Note: this is a numeric value!
account_number_str = str(123) # And this is the number, as text
account_number_str.isdigit() # Returns True - only contains digits
account_number_str.isalpha() # Returns False - doesn't contain letters
# Right-justify out to a length of 6, with zero characters:
account_number_str.rjust(6, "0") # 123 becomes "000123"
Python also has a powerful “string interpolation” feature in the form of f-strings: Adding a lowercase f
to the beginning of a string will allow you to embed other python variables and expressions into a string, by including the variable names and other Python code in {curly braces}:
first_name = "Jane"
last_name = "Smith"
full_name = f"{last_name}, {first_name}"
# full_name is now "Smith, Jane"
We can combine these features to accomplish some very complicated transformations in relatively little code:
account_number = record.get("AccountNumber") # for example: 425
account_number = str(account_number).rjust(5, "0") # left-pad with zeroes
# account_number would now be: "00425"
item_code = record.get("ItemCode") # for example: "dac"
record["sku"] = f"SKU-{account_number}-{item_code.upper()}"
# record["sku"] would now be: "SKU-00425-DAC"
To break this down:
record
:record["sku"]
field is now set to "SKU-00425-DAC"
This is just scratching the surface of what Python provides, but hopefully we've given you some examples how you can use it in some Transforms of your own.
Thanks for reading. Matterbeam transforms provide users with the control and flexibility to fully customize their data transformations. We're continuously working to expand functionality, so stay tuned for even more powerful Python-based data transformation features in the future!