Iterparse and ElementTree confusion
Hi Im trying to parse a large(150MB) xml file in order to extract specific required records. import sys from elementtree.ElementTree import ElementTree root = ElementTree(file=big.xml') This works fine for smaller versions of the same xml file but...when i attempted the above my PC goes to lala land, theres much HDD grinding followed by "windows runnign low on virtual memory" popup after 10-15mins. Then just more grinding...for an hour before i gave up XML file format: . . . . . . 23172 Three Spot Rail Light Brushed Chrome 73520 . .etc Ok, i thought, surely theres a way to parse this thing in chucnks till i get to the element i require then I'll reuse the ElementTree goodness. I found Iterparse def parse_for_products(filename): for event, elem in iterparse(filename): if elem.tag == "Products": root = ElementTree(elem) print_all(root) else: elem.clear() My problem is that if i pass the 'elem' found by iterparse then try to print all attributes, children and tail text i only get elem.tagelem.keys returns nothing as do all of the other previously useful elementtree methods. Am i right in thinking that you can pass an element into ElementTree? How might i manually iterate through ... grabbing everything? -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterparse and ElementTree confusion
Further to the above I pass my found element 'Product' to ElementTree's handy print all function def print_all(element): root = element #Create an iterator iter = root.getiterator() #Iterate for element in iter: #First the element tag name print "Element:", element.tag #Next the attributes (available on the instance itself using #the Python dictionary protocol if element.keys(): print "\tAttributes:" for name, value in element.items(): print "\t\tName: '%s', Value: '%s'"%(name, value) #Next the child elements and text print "\tChildren:" #Text that precedes all child elements (may be None) if element.text: text = element.text text = len(text) > 40 and text[:40] + "..." or text print "\t\tText:", repr(text) if element.getchildren(): #Can also use: "for child in element.getchildren():" for child in element: #Child element tag name print "\t\tElement", child.tag #The "tail" on each child element consists of the text #that comes after it in the parent element content, but #before its next sibling. if child.tail: text = child.tail text = len(text) > 40 and text[:40] + "..." or text print "\t\tText:", repr(text) if the element i pass to the above is from root = ElementTree(file='somefile.xml') iter = root.getiterator() #Iterate for element in iter: if element.tag == 'Product': print_all(element) I print all of my required element's attributes, children, children's children, etc However if i pass the element i get from iterparse for event, elem in iterparse(filename): if elem.tag == "Products": print_all(elem) else: elem.clear() i only get the attributes for and a list of its children, no further iteration into the children and the children's children what am i missing? -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterparse and ElementTree confusion
Ofcourse! Thankyou so much. All is well, using the Iterparse takes a mere 3mins to find the last product in the xml and this is probably due to my checking 1 products for a specific. I feel rather honoured to have 'the' effbot reply to my humble post Thanks again Paul -- http://mail.python.org/mailman/listinfo/python-list
Disable 'windows key'
Hi Ive made a simple game for my 10 month old son. I noticed that (having watched daddy) he loved to bash the keyboard whenever possible. So the game simply displays various pictures of animals, accompanied by the animal sound and printed name, whenever any keys are bashed 3 times. He loves it. However, frantic bashing tends to mean that the 'windows key' invariably gets hit thus causing the game to drop to desktop. I wonder if there might be a way of disabling these keys within my program. I realise there is a way of disabling them in the windows registry, but i do actually use it for other things (namely winKey+e to explore) so would prefer not having to do that. OS: Win XP regards Paul ps. if anyone has little 'uns and would like a copy of the source just drop me a line. -- http://mail.python.org/mailman/listinfo/python-list