Python Benedict

0.35.0 · active · verified Sat Apr 11

python-benedict is a `dict` subclass that enhances the standard Python dictionary with powerful features like keylist, keypath, and keyattr support. It provides normalized I/O operations for various data formats (including base64, csv, ini, json, pickle, plist, query-string, toml, xls, xml, yaml) and numerous utility methods. It is currently active, with its latest version being 0.35.0, and maintains a regular release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates creating a `benedict` instance, accessing and setting values using both keypath (dot notation) and keylist (list of keys), and serializing the dictionary to a JSON string.

from benedict import benedict

# Create a benedict instance from a dictionary
data = {
    "user": {
        "profile": {
            "name": "Alice",
            "email": "alice@example.com"
        },
        "id": 123
    },
    "settings": {
        "theme": "dark"
    }
}
d = benedict(data)

# Access values using keypath (dot notation)
print(f"User Name: {d['user.profile.name']}")

# Set values using keypath
d['user.profile.age'] = 30
print(f"User Age: {d.get('user.profile.age')}")

# Access using keylist (list of keys)
print(f"User ID: {d[['user', 'id']]}")

# Output to JSON string
json_output = d.to_json(indent=2)
print("\nJSON Output:")
print(json_output)

view raw JSON →