Re: [Tutor] XML and ElementTree

2016-04-26 Thread Alan Gauld via Tutor
Forwarding to tutor list.
Please always use "Reply All" or "Reply List" when responding to the
tutor list.

On 26/04/16 09:24, Marco Soldavini wrote:
> On Tue, Apr 26, 2016 at 2:05 AM, Alan Gauld via Tutor  
> wrote:
>> On 25/04/16 17:24, Marco Soldavini wrote:
>>> I've a few questions about parsing XML. I wrote some code that works
>>> but I want to know which are the most intelligent data structures to
>>> parse data to
>> Answer: The ones that suit what you want to do with it.
>> But you haven't told us what you plan on using it for so
>> we can't tell you what is suitable.
>>
> Let's say I would use the tag values as argument of function call in
> the subsequent part of the program.
>
> For example one tag will contain a server address. In the program I'll
> have a function which will use this address to perform a connection.
>


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XML and ElementTree

2016-04-25 Thread Alan Gauld via Tutor
On 25/04/16 17:24, Marco Soldavini wrote:
> I've a few questions about parsing XML. I wrote some code that works
> but I want to know which are the most intelligent data structures to
> parse data to

Answer: The ones that suit what you want to do with it.
But you haven't told us what you plan on using it for so
we can't tell you what is suitable.

> 
> 
> 1
> XXX
> 
>
>
>
>
>
>
> 
> 
> 
> 
> 
> 
> 
> So root element can have station child element from 1 to n
> Each station element have unique child elements apart from groups
> which may have more group child element each with a few tags
> 
> Now I want to parse this xml file and get the tag values into
> variables in python
> I could have 1 station element or 2 or 3

So you could have a list or a dictionary of Stations.
Each station might be simple values, or tuples, or dictionaries
or an instance of a Station class.

> I could append all data in a long list, but is this good?

It all depends on what you want to do.
If you just want to process all the stations sequentially then yes,
a long list is good. (Unless its too long in which case you may
be better with a database or a flat file.)

> If later in the program I want to access a variable value I want to do
> it with the xml tag name and not with an index like Settings[10] for
> example but something like Settings['tag']

That suggests a dictionary or class would be best. (Although
a named tuple or database may also be appropriate.)

> But what if I have more than one structure like station which has the same 
> keys?

Figuring out object identity is always tricky when the data
is similar. a Database solution offers row ids to disambiguate similar
entries. A class based solution has an id (usually the memory address),
a list has its index. Its really up to you to determine the best
option based on your needs.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] XML and ElementTree

2016-04-25 Thread Marco Soldavini
I've a few questions about parsing XML. I wrote some code that works
but I want to know which are the most intelligent data structures to
parse data to

Consider that my XML file is something like the following:




1
XXX

   
   
   
   
   
   







So root element can have station child element from 1 to n
Each station element have unique child elements apart from groups
which may have more group child element each with a few tags

Now I want to parse this xml file and get the tag values into
variables in python
I could have 1 station element or 2 or 3
I could append all data in a long list, but is this good?
Could it be better to build a dictionary for each station element and
then build a list of dictionary
Which method you suggest to find data and use it after parsing it?

If later in the program I want to access a variable value I want to do
it with the xml tag name and not with an index like Settings[10] for
example but something like Settings['tag']

But what if I have more than one structure like station which has the same keys?

Following is part of my code for now.
Thanks!
marco


try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET


# Settings XML File Parsing

tree = ET.parse('settingstest.xml')
root = tree.getroot()

stations = len(root)
print "Found ",stations, " stations configured in settings file"

Settings = []
for station in root:
   StationId = station.find('StationId')
   Settings.append(StationId)
   .
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor