Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> import anydbm
- >>> db = anydbm.open('captions.db', 'c')
- # The mode 'c' means that the database should be created if it doesnโt already exist.
- # The result is a database object that can be used (for most operations) like a dictionary.
- # If you create a new item, anydbm updates the database file:
- >>> db['felicity.png'] = 'Photo of Felicity Smoak.'
- # When you access one of the items, anydbm reads the file:
- >>> print db['felicity.png']
- Photo of Felicity Smoak.
- # If you make another assignment to an existing key, anydbm replaces the old value:
- >>> db['felicity.png'] = 'Photo of Felicity Smoak falling asleep on her laptop.'
- >>> print db['felicity.png']
- Photo of Felicity Smoak falling asleep on her laptop.
- # Many dictionary methods, like keys and items, also work with database objects. So does iteration with a for statement.
- >>> for key in db:
- print key
- As with other files, you should close the database when you are done:
- >>> db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement