I have a dict containing a name in two parts:
target = {
"givenName": "Elvis",
"middleName": "Aron",
}
middleName
is optional. I need to map to them one string, which is either givenName + ' ' + middleName
if middleName
is defined or just firstName
. What is the canonical way to do this using glom
?
The solution I've found so far is to use lambda
and Coalesce
. The below solution will work whether middleName
is included or not:
glom(target, Coalesce(
lambda t: t["givenName"] + " " + t["middleName"],
"givenName",
skip_exc=KeyError),
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.