def _file_to_tree(_data_format, _reference):
"""Reads a file and chooses the right parser to make it an lxml element tree"""
print("format_to_tree : " + _data_format)
if _data_format == 'HTML':
from lxml import html
return html.parse(_reference)
if _data_format == 'XML':
from lxml import etree
return etree.parse(_reference)
if _data_format == 'JSON':
from lxml import etree
from json_lxml import element
with open(_reference, "r") as _f:
_top_element = json.load(_f)
return etree.ElementTree(element("top",_top_element))
else:
raise Exception("_file_to_tree: " + _data_format + " is not supported")
评论列表
文章目录