Python XML Parsing Tutorial Read And Write XML Files In Python

Поделиться
HTML-код
  • Опубликовано: 20 окт 2024

Комментарии • 2

  • @maltesh105joshi
    @maltesh105joshi Год назад

    I have xml and i can sort within first child only. but it will through an error when the line have commented in my 1st child. it works fine if there is NO comments. can you help me here
    CODE
    root = tree.getroot()
    for c in root:
    c[:] = sorted(c, key=lambda child: (child.tag,child.get('name')))
    xmlstr = ET.tostring(root, encoding="utf-8", method="xml")
    print(xmlstr.decode("utf-8"))
    ERROR
    c[:] = sorted(c, key=lambda child: (child.tag,child.get('name')))
    TypeError: '

    • @CodersArcade
      @CodersArcade  Год назад

      The error you're encountering is because you are trying to sort the child elements of `c` using a lambda function as the sorting key. The error message indicates that you cannot use the '