python - storing output to a list or dictionary -


i'm using following code portion of larger program error checking on digital cinema package , tries check validity of xml file lists asses on dcp. anyway, still in infancy , i'm hoping learn more python result of it.

import xml.etree.elementtree etree import sys  class parser(object):     def __init__(self, file_name):         self.file_name = file_name      def display(self, rename_this_list):         tree = etree.parse(self.file_name)         node in tree.getiterator():             element in rename_this_list:                 if element in node.tag:                     uuid=(node.text)                     #uuid = [s.strip('urn:') s in uuid]                     print(uuid)  fname = sys.argv[1] key_search_words = ['keyid'] instance = parser(fname) instance.display(key_search_words) 

when try store output each line list doesn't format way expect. minus urn: i'd storing each line uuid: , following info element of list.

urn:uuid:9851b0f6-4790-0d4c-a69d-ea8abdedd03d urn:uuid:8317e8f3-1597-494d-9ed8-08a751ff8615 urn:uuid:5d9b228d-7120-344c-aefc-840cdd32bbfc urn:uuid:1e32ccb2-ab0b-9d43-b879-1c12840c178b urn:uuid:44d04416-676a-2e4f-8995-165de8cab78d urn:uuid:906da0c1-b0cb-4541-b8a9-86476583cdc4 urn:uuid:0fe2d73a-ebe3-9844-b3de-4517c63c4b90 urn:uuid:862fa79a-18c7-9245-a172-486541bef0c0 urn:uuid:aa2f1a88-7a55-894d-bc19-42afca589766 urn:uuid:59d6eeff-cd56-6245-9f13-951554466626 urn:uuid:14a13b1a-76ba-764c-97d0-9900f58af53e urn:uuid:ccdbe0ae-1c3f-224c-b450-947f43bbd640 urn:uuid:dcd37f10-b042-8e44-bef0-89bda2174842 urn:uuid:9dd7103e-7e5a-a840-a15f-f7d7fe699203 

if need list, can try this.

    def display(self, rename_this_list):         listofnodes = []         tree = etree.parse(self.file_name)         node in tree.getiterator():             element in rename_this_list:                 if element in node.tag:                    # append text of element list                     # without first 4 characters "urn:"                    listofnodes.append(node.text[4:])         print str(listofnodes)         return listofnodes 

remember keys of dictionary have unique, in dictionary can't have 2 items keys "uuid", if want dictionary can have 1 dictionary 1 key "uuid" , list of numbers values.


Comments