D
D
dmitriyprog2018-01-27 17:49:24
Python
dmitriyprog, 2018-01-27 17:49:24

How to remove element from xml?

Writes that ValueError: list.remove(x): x not in list

def remove_number(self, number):
       try:
            number = str(number).replace('\'','')
            print("REMOVE THE Number " + number)
            file = open('bot/test.xml', 'r')
            tree = ET.parse(file)
            root = tree.getroot()
            elem = tree.find("numbers")
            for child in elem:
                print("Child")
                print(child.text)
                print(child.text + " " + str(number))
                print(child.text.strip() == str(number).strip())
                if child.text.strip() == str(number).strip():
                    root.remove(child)
                    print("Success")
            tree.write('bot/test.xml')
            return
       except FileNotFoundError:
           print("File not found")

.........
<root>
...
  <numbers>
      <number>79043358876</number>
      <number>79043360876</number>
      <number>79043363501</number>
      <number>79043371799</number>
      <number>79043370024</number>
      <number>79043368917</number>
      <number>79043360041</number>
      <number>79043370209</number>
  </numbers>
</root>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question