Writing to Records - There are a few different ways that we can modify an existing record, to add a new field and value, change the value on an existing field, or remove a field entirely.
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
There are a few different ways that we can modify an existing record, to add a new field and value, change the value on an existing field, or remove a field entirely. (And of course, we can combine these operations to make more complex changes, like renaming a field…)
The square bracket notation we used to read from a record can also be used to write to it:
record["AccountType"] = "(legacy)"
==
, are used for comparison and equality checks - A single equal sign, =
, is used for assignment. It may be a little confusing if you aren’t used to it, but this convention appears in many other programming languages...This will set the “AccountType” field to “(legacy)” on this record - Because the “keys” of Python dictionaries are unique values, this would overwrite an existing “AccountType” field, or create a new one, if this record didn’t already have an “AccountType”.
While we can assign values to variables, we could also both read from and write to a field in the same line. (Python conveniently lets us use the + operator to combine strings of text):
record["AccountType"] = record["AccountType"] + "-legacy"
# if record["AccountType"] was "B", it would now be "B-legacy"
This will read the current value of “AccountType” from record, add “-legacy” to the end of the value, and write it back into the “AccountType” field.
However, if we’re not sure the “AccountType” field is always going to be present, we might want to combine this with use of the “get” method, so we can give missing fields a default value:
record["AccountType"] = record.get("AccountType", "N/A") + "-legacy"
# As above, but if AccountType is missing, defaults to "N/A-legacy"
If you simply want to add to the end of of a piece of text, you can also abbreviate the "a = a + b" syntax, by using the +=
operator:
record["AccountType"] += "-legacy"
# Adds "-legacy" to the end of the "AccountType" field...
# But only if it already exists, and is a string of text...
The +=
operator is a shortcut to assigning something to itself, plus something else, which can make our code shorter, and cleaner - But that simplicity does come with some assumptions - In this case, the "AccountType" field must already exist in our record
for this code to work properly.
While Python lets you use +
with text as well as numbers and other objects, it usually only works with objects of the same type: You can use +
to combine two pieces of text, or two numbers, but not text and a number.
These examples will work:
record["full_name"] = "first_name" + "," + "last_name"
record["total_purchases"] = 10
record["total_purchases"] += 1
But these examples will not:
# WARNING: BAD EXAMPLES! DO NOT USE!
record["full_name"] = "first_name" + 2 + "last_name"
record["total_purchases"] = 10 + "items"
# Results in errors similar to:
# TypeError: Can only concatenate str (not "int") to str
Continue reading part 4 of this series here, where we dive into removing fields from a record.