xml processing

Discussion in 'Python' started by Jeff Elkins, Jun 1, 2005.

  1. Jeff Elkins

    Jeff Elkins Guest

    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
     
    Jeff Elkins, Jun 1, 2005
    #1
    1. Advertising

  2. Jeff Elkins

    Magnus Lycka Guest

    Jeff Elkins wrote:
    > 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:
    >>> import pickle
    >>> f2 = open('phonebook')
    >>> l = pickle.load(f2)
    >>> f2.close()
    >>> for item in l:

    print item


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

    The file looks like this:
    >>> print open('phonebook').read()

    (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.
     
    Magnus Lycka, Jun 1, 2005
    #2
    1. Advertising

  3. Jeff Elkins

    Jeff Elkins Guest

    On Wednesday 01 June 2005 09:51 am, Magnus Lycka wrote:
    > Jeff Elkins wrote:
    > > 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.


    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
     
    Jeff Elkins, Jun 1, 2005
    #3
  4. Jeff Elkins wrote:
    > 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
     
    Steven Bethard, Jun 1, 2005
    #4
  5. Jeff Elkins

    Jeff Elkins Guest

    On Wednesday 01 June 2005 11:01 am, Steven Bethard wrote:

    > 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.


    Thanks. I've installed it and am experimenting.
     
    Jeff Elkins, Jun 2, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bomb Diggy
    Replies:
    0
    Views:
    448
    Bomb Diggy
    Jul 28, 2004
  2. Oleg  Paraschenko
    Replies:
    0
    Views:
    381
    Oleg Paraschenko
    Jun 6, 2005
  3. knorth
    Replies:
    0
    Views:
    364
    knorth
    Nov 5, 2005
  4. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    516
    Michael Foord
    Sep 17, 2004
  5. Tomasz Wegrzanowski

    magic/xml library for easy XML processing

    Tomasz Wegrzanowski, Aug 5, 2006, in forum: Ruby
    Replies:
    5
    Views:
    95
    Tomasz Wegrzanowski
    Aug 6, 2006
Loading...

Share This Page