0

Let us suppose. I have xml data, which is not well-formed. Can I print own message like:

xml is not well performed

.

python code

import xml.etree.ElementTree as ET
root = ET.parse('data.xml').getroot()
# ParseError: mismatched tag: line 8, column 26
print('xml is not well performed')
3
  • 3
    You can wrap your ET.parse in a try-except block. If an exception is raised, print a message. Commented Apr 6, 2021 at 11:51
  • try: except ParseError: Commented Apr 6, 2021 at 11:52
  • docs.python.org/3/tutorial/errors.html Commented Apr 6, 2021 at 11:54

1 Answer 1

1

try this:

import xml.etree.ElementTree as ET
try:
    root = ET.parse('data.xml').getroot()
except:
    print('xml is not well performed')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.