XML File -- dictionary edit/search

Nagarajan naga86 at gmail.com
Tue Aug 28 00:55:57 EDT 2007


On Aug 28, 6:15 am, -b <bradfo... at gmail.com> wrote:
> I am trying to put together a python program that will be able to ask
> for a word and its definition, then save that information into an xml
> file for reference. I am not down right set on the file being in xml
> format, but xml is the first thing that comes to mind, since I do not
> want to use a MySQL database. I think xml will be easy to search and
> return a word's definition--another reason why xml comes to mind first
> after MySQL databases. Please, if you have any suggestions to what
> python-xml libraries I might use to program this small app with, then
> by all means voice your thoughts. Thank you very much for your time.

A simple yaml file might just do the trick.
Your yaml file shall look like the following:

Word-def.yaml
word1: word1's definition
word2: word2's definition
..
..
..
Use pyyaml to handle yaml files.
--------
import yaml
worddefs = yaml.load( open( "word-def.yaml", "r" ).read() )
print worddefs
--------

worddefs will be a dictionary of words (with definitions as values, of
course). You cannot get a better representation.
To add a definition, you can as well add keys to the worddefs
dictionary and use "dump" method to write to the yaml file. Or you
could just write to the file directly (as the format is simple in this
case). But I suggest former.

Did this solve your problem?


-Brevity is the soul of wit.
Nagarajan.




More information about the Python-list mailing list