Books Database

Alex Martelli aleax at aleax.it
Mon Mar 3 16:31:34 CET 2003


Eliran Gonen wrote:

> I want to ask you what do you think is the best way to keep
> a list of books with other data (such as year and author) in a
> file ?

That depends on your purposes and on the size the database
is likely to grow to.

> And what way should be used to parse it ?

That depends on what format you're keeping it in.


> Is something like:
> 
> <.database>
> 
> name=Example
> author=Somedood
> year=1969
> 
> name=Wuffy
> author=Puffy
> year=2010
> 
> ....etc
> </.databse>
> 
> Is alright ?

I don't like it at all -- neither fish nor fowl, and you're
likely to have to write your own parser for it -- not trivial,
though not a really big deal -- naah.

XML is probably OK:

<database>

<book name="Example" author="Somedood" year="1969"/>
<book name="Wuffy" author="Puffy" year="2010"/>

</database>

and you can parse it easily with SAX, for example.


Or if the fields you want to record for each book are fixed
and you "know" they'll never contain (e.g.) newlines and
vertical bars, you could use a one-line-per-record, fields-
separated-by-vbars format:

Example|Somedood|1969
Wuffy|Puffy|2010

and "parse" this with [ rec[:-1].split('|') for rec in afile ].


Alex





More information about the Python-list mailing list