xml processing

J

Jeff Elkins

I've like to use python to maintain a small addressbook which lives on a Sharp
Zaurus. This list will never grow beyond 200 or so entries. I've installed
pyxml.

Speaking generally, given a wxpython app to do data entry,
I'm planning to:

1. parse the addressbook file, loading its data into an array.
2. Perform any edit operations within the array.
3. Write out a finished xml file from the array when I'm done.

Is this reasonable? Better, smarter ways to accomplish this?

Thanks for any advice.

Jeff Elkins
 
M

Magnus Lycka

Jeff said:
I've like to use python to maintain a small addressbook which lives on a Sharp
Zaurus. This list will never grow beyond 200 or so entries. I've installed
pyxml.

Speaking generally, given a wxpython app to do data entry,
I'm planning to:

1. parse the addressbook file, loading its data into an array.
2. Perform any edit operations within the array.
3. Write out a finished xml file from the array when I'm done.

Is this reasonable? Better, smarter ways to accomplish this?

Why XML?

I guess the simplest solution whould be to use pickle.

Saving:
>>> import pickle
>>> l = []
>>> l.append(('Alan', '1st Street', 123456))
>>> l.append(('Ben', '2nd Street', 234567))
>>> l.append(('Clark', '3rd Street', 345678))
>>> f = open('phonebook','w')
>>> pickle.dump(l, f)
>>> f.close()
Loading:
print item


('Alan', '1st Street', 123456)
('Ben', '2nd Street', 234567)
('Clark', '3rd Street', 345678)

The file looks like this:(lp0
(S'Alan'
p1
S'1st Street'
p2
I123456
tp3
a(S'Ben'
p4
S'2nd Street'
p5
I234567
tp6
a(S'Clark'
p7
S'3rd Street'
p8
I345678
tp9
a.
Ok, the file content might not seem completely obvious, but it's
not really so difficult to parse it, and it's certainly less verbose
than XML. Above all, much less code.

BTW, cPickle is faster than pickle, but I suspect it doesn't matter
with such a small amount of data. It's easy to replace "import pickle"
with "import cPickle as pickle" to try it out.
 
J

Jeff Elkins

Why XML?

I guess the simplest solution whould be to use pickle.

The Zaurus addressbook app depends on an xml datafile, so I'm stuck with that
format. I just want to to maintenence and data entry on the PC for ease of
use vs typing on the Zaurus' tiny keyboard. I could just edit the raw xml
file, then copy it to the Zaurus, but I'd like to have something a little
spiffier...

Jeff
 
S

Steven Bethard

Jeff said:
I've like to use python to maintain a small addressbook which lives on a Sharp
Zaurus. This list will never grow beyond 200 or so entries. I've installed
pyxml.

If you're not committed to pyxml, you might consider using ElementTree:

http://effbot.org/zone/element-index.htm

I find it *way* easier to work with.
Speaking generally, given a wxpython app to do data entry,
I'm planning to:

1. parse the addressbook file, loading its data into an array.
2. Perform any edit operations within the array.
3. Write out a finished xml file from the array when I'm done.

Is this reasonable? Better, smarter ways to accomplish this?

Seems pretty reasonable. Another option might be to parse the
addressbook file into an XML object and then modify the XML object
itself. E.g.:

tree = ElementTree(file="...")
elem = tree.getroot()
for node in elem.findall("..."):
node.text = "..."

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top