Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''import xml.etree.ElementTree as ET
- tree = ET.parse('xml1.xml')
- root = tree.getroot()
- for i in root.findall('./shop/'):
- print(i)'''
- '''import xml.etree.ElementTree as ET
- p = 0
- tovar_id = None
- tree = ET.parse('xml2.xml')
- root = tree.getroot()
- for i in root.findall('.//offers/offer/price'):
- #print(i.items)
- if float(i.text) > p:
- p = float(i.text)
- p = str(p)
- #print(p)
- nodes = []
- for child_node in root.findall('.//offers/'):
- node = child_node
- for i in node.findall('.//price'):
- if i.text == p:
- the_node = node
- nodes.extend(node.findall('.//categoryId'))
- #.findall('.//categoryId'))
- ##.findall('.//categoryId').attrib)
- id = []
- for node in nodes:
- id.append(node.text)
- print(id)
- nazvania = []
- for node in root.findall('.//categories/'):
- for i in id:
- if node.attrib['id']== i:
- nazvania.append(node.text)
- for i in sorted(nazvania):
- print(i)'''
- import xml.etree.ElementTree as ET
- p = 0
- tovar_id = None
- tree = ET.parse('xml5.xml')
- root = tree.getroot()
- categories = root.findall('.//categoryId')
- set_c = set()
- for i in categories:
- set_c.add(i.text)
- print(set_c)
- mdict = {i:[0, 0] for i in set_c}
- for i in mdict.keys():
- for node in root.findall('.//offers/'):
- for j in node.findall('./categoryId'):
- if j.text == i:
- mdict[i][0] += float(node.find('./price').text)
- mdict[i][1] += 1
- for j in mdict.keys():
- for i in root.findall('.//categories/'):
- if j == i.attrib['id']:
- mdict[j].append(i.text)
- for i in mdict.values():
- i[0] = i[0]/i[1]
- for i in sorted(mdict.values(), key=lambda x:x[-1]):
- sv = ', '.join(str(j) for j in i[::-1])
- print(sv)
- #print(i.items)
- '''p = str(p)
- #print(p)
- nodes = []
- for child_node in root.findall('.//offers/'):
- node = child_node
- for i in node.findall('.//price'):
- if i.text == p:
- the_node = node
- nodes.extend(node.findall('.//categoryId'))
- #.findall('.//categoryId'))
- ##.findall('.//categoryId').attrib)
- id = []
- for node in nodes:
- id.append(node.text)
- print(id)
- nazvania = []
- for node in root.findall('.//categories/'):
- for i in id:
- if node.attrib['id']== i:
- nazvania.append(node.text)
- for i in sorted(nazvania):
- print(i)'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement