Guest User

Untitled

a guest
Jun 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1.  
  2. def importEtree():
  3.     try:
  4.       from lxml import etree
  5.       print("running with lxml.etree")
  6.     except ImportError:
  7.       try:
  8.         # Python 2.5
  9.         import xml.etree.cElementTree as etree
  10.         print("running with cElementTree on Python 2.5+")
  11.       except ImportError:
  12.         try:
  13.           # Python 2.5
  14.           import xml.etree.ElementTree as etree
  15.           print("running with ElementTree on Python 2.5+")
  16.         except ImportError:
  17.           try:
  18.             # normal cElementTree install
  19.             import cElementTree as etree
  20.             print("running with cElementTree")
  21.           except ImportError:
  22.             try:
  23.               # normal ElementTree install
  24.               import elementtree.ElementTree as etree
  25.               print("running with ElementTree")
  26.             except ImportError:
  27.               print("Failed to import ElementTree from any known place")
  28.  
  29.  
  30. importEtree()
  31.  
  32. tree = etree.parse('hero.entity')
  33.  
  34. root = tree.getroot()
  35.  
  36.  
  37. print root
  38. print tree
  39.  
  40.  
  41. :::::::OUTPUT:::::::
  42.  
  43. running with cElementTree on Python 2.5+
  44.  
  45. Traceback (most recent call last):
  46.   File "C:\Users\echelon\Desktop\python\XLMtest.py", line 32, in <module>
  47.     tree = etree.parse('hero.entity')
  48. NameError: name 'etree' is not defined
  49. >>>
Add Comment
Please, Sign In to add comment