Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. import xml.etree.ElementTree as et
  2.  
  3. # Парсим файлы
  4. file1 = et.ElementTree().parse('1.xml')
  5. file2 = et.ElementTree().parse('2.xml')
  6.  
  7. a_tag = file1.find('a') # Ищем тег 'a'
  8.  
  9. # Пихаем в file2 тег 'a' с теми же атрибутами, что и у 'a' из первого файла
  10. new_a = et.SubElement(file2, 'a', a_tag.attrib)
  11.  
  12. b_tags = file2.findall('b') # 'Ищем теги b' из второго файла
  13.  
  14. # Добавляем теги 'b' в 'a' и удаляем их из корня
  15. for b in b_tags:
  16.     et.SubElement(new_a, 'b', b.attrib).text = ''
  17.     file2.remove(b)
  18.  
  19. # Пишем это всё в новый файл
  20. tree = et.ElementTree(file2)
  21. tree.write('3.xml', encoding='UTF-8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement