# XML Parser by Python with VS Code ###### tags: `Python` * 安裝Python * 安裝Visual Code * 更新pip ``` python.exe -m pip install --upgrade pip ``` * 安裝Code Runner ![](https://i.imgur.com/25XwPz2.png) * 設定Code Runner執行VS Code的terminal ![](https://i.imgur.com/m8Vm7Y7.png) * 安裝BeautifulSoup4 ``` pip install beautifulsoup4 ``` * 安裝lxml parser ``` pip install lxml ``` * 安裝 openpyxl ``` pip install openpyxl ``` * 安裝 auto-py-to-exe ``` pip install auto-py-to-exe ``` --- #### 處理XML換行、縮排 ``` def indent(elem, level=0): i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i ``` --- The ElementTree XML API https://docs.python.org/2/library/xml.etree.elementtree.html BeautifulSoup4 Documentation https://www.crummy.com/software/BeautifulSoup/bs4/doc/ The lxml.etree Tutorial https://lxml.de/tutorial.html