Advertisement
AlexandraWTF

Untitled

May 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. ''import xml.etree.ElementTree as ET
  2.  
  3. tree = ET.parse('xml1.xml')
  4. root = tree.getroot()
  5.  
  6. for i in root.findall('./shop/'):
  7.     print(i)'''
  8.  
  9.  
  10. '''import xml.etree.ElementTree as ET
  11. p = 0
  12. tovar_id = None
  13. tree = ET.parse('xml2.xml')
  14. root = tree.getroot()
  15.  
  16. for i in root.findall('.//offers/offer/price'):
  17.     #print(i.items)
  18.     if float(i.text) > p:
  19.         p = float(i.text)
  20. p = str(p)
  21. #print(p)
  22. nodes = []
  23. for child_node in root.findall('.//offers/'):
  24.     node = child_node
  25.     for i in  node.findall('.//price'):
  26.         if i.text == p:
  27.             the_node = node
  28.             nodes.extend(node.findall('.//categoryId'))
  29.                          #.findall('.//categoryId'))
  30.             ##.findall('.//categoryId').attrib)
  31. id = []
  32. for node in nodes:
  33.     id.append(node.text)
  34. print(id)
  35.  
  36.  
  37. nazvania = []
  38. for node in root.findall('.//categories/'):
  39.     for i in id:
  40.         if node.attrib['id']== i:
  41.             nazvania.append(node.text)
  42. for i in sorted(nazvania):
  43.     print(i)'''
  44.  
  45. import xml.etree.ElementTree as ET
  46. p = 0
  47. tovar_id = None
  48. tree = ET.parse('xml5.xml')
  49. root = tree.getroot()
  50. categories = root.findall('.//categoryId')
  51. set_c = set()
  52. for i in categories:
  53.    set_c.add(i.text)
  54. print(set_c)
  55. mdict = {i:[0, 0] for i in set_c}
  56.  
  57. for i in mdict.keys():
  58.    for node in root.findall('.//offers/'):
  59.        for j in node.findall('./categoryId'):
  60.            if j.text == i:
  61.                mdict[i][0] += float(node.find('./price').text)
  62.                mdict[i][1] += 1
  63.  
  64. for j in mdict.keys():
  65.    for i in root.findall('.//categories/'):
  66.        if j == i.attrib['id']:
  67.            mdict[j].append(i.text)
  68. for i in mdict.values():
  69.    i[0] = i[0]/i[1]
  70. for i in sorted(mdict.values(), key=lambda x:x[-1]):
  71.    sv = ', '.join(str(j) for j in i[::-1])
  72.    print(sv)
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.    #print(i.items)
  81.  
  82. '''p = str(p)
  83. #print(p)
  84. nodes = []
  85. for child_node in root.findall('.//offers/'):
  86.     node = child_node
  87.     for i in  node.findall('.//price'):
  88.         if i.text == p:
  89.             the_node = node
  90.             nodes.extend(node.findall('.//categoryId'))
  91.                          #.findall('.//categoryId'))
  92.             ##.findall('.//categoryId').attrib)
  93. id = []
  94. for node in nodes:
  95.     id.append(node.text)
  96. print(id)
  97.  
  98.  
  99. nazvania = []
  100. for node in root.findall('.//categories/'):
  101.     for i in id:
  102.         if node.attrib['id']== i:
  103.             nazvania.append(node.text)
  104. for i in sorted(nazvania):
  105.     print(i)'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement