Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created March 29, 2025 18:58
Show Gist options
  • Save ncalm/5916959ae525b694f6042a006814d905 to your computer and use it in GitHub Desktop.
Save ncalm/5916959ae525b694f6042a006814d905 to your computer and use it in GitHub Desktop.
Function to extract properties from Geography RichValue type in Python in Excel
def flatten_geography(rich_value):
props = rich_value.data.get("properties", {})
flat = {}
def extract(val):
if not isinstance(val, dict):
return str(val) if val is not None else None
typ = val.get("type")
if typ in ("String", "FormattedNumber"):
return val.get("basicValue")
elif typ == "LinkedEntity":
return val.get("text")
elif typ == "WebImage":
return val.get("address")
else:
return None
def flatten_list(val_list):
result = []
for item in val_list:
if isinstance(item, list):
result.extend(flatten_list(item)) # flatten deeper
elif isinstance(item, dict):
result.append(extract(item))
else:
result.append(str(item)) # likely a string or number
return result
for key, val in props.items():
if isinstance(val, list):
flat[key] = flatten_list(val)
elif isinstance(val, dict):
flat[key] = extract(val)
else:
flat[key] = str(val) if val is not None else None
return flat
"Function to extract geography properties"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment