借貸管道 by 儲蓄保險王 · 2015-08-23 相關文章Python XML 的生死輪迴:深入理解 lxml.etree.fromstring(xml_bytes) #Bytes → Element Object 與 lxml.etree.tostring(Element, encoding= "utf-8") #Element Object → Bytes ; 處理 XML 時,盡量全程保持 Bytes (二進位) 狀態。告別雜亂 XML!用 Python lxml 實現與 VS Code (Shift+Alt+F) 同級的「完美縮排」; from lxml import etree ; root = etree.fromstring(xml_bytes) #等效 root = etree.fromstring( xml_str.encode("utf-8") ); clean_xml_str = etree.tostring(root, pretty_print=True, encoding='unicode', xml_declaration=False) ; import xml.etree.ElementTree as ETPython DOCX 手術刀:精準切片與瘦身 (OOXML 實戰); from lxml import etree; doc_xml = zfin.read('word/document.xml') ; doc_tree = etree.fromstring(doc_xml) ; used_rids = set( doc_tree.xpath( "//@r:embed | //@r:link | //@r:id", namespaces=ns_map)) #獲取 word/document.xml 有使用的used_rids => 讀取 document.xml.rels 建立白名單 keep_files以及黑名單 rels_to_remove ,要移除的Relationship節點 => 從 XML 樹中移除未使用的 Relationship 節點 => 重寫 Zip (過濾孤兒檔案, 更新document.xml.rels,其他原樣複製)Python Regex 實戰:精準抓取 XML 屬性值 (findall vs finditer 與 Group 的奧義)python-docx 進階手術室:從高階 API 到底層 XML (w:p, w:r, w:t) 完全解析; from docx.oxml import OxmlElement ; from docx.oxml.ns import qn如何使用國O證券紅財神一鍵買進800支零股?領取股東紀念品,綁定樂天銀行(推薦序號: HBUG09,新戶送500元現金),國O證券送600元手續費抵用金Python hashlib 快速入門:為資料建立唯一指紋import hashlib; data = "文字".encode('utf-8'); hash_obj = hashlib.sha256(data); result = hash_obj.hexdigest() #digest:「取得雜湊摘要」或「取得摘要值」Python-docx 進階手術室:從底層 XML 到超連結混合技; from docx.opc.constants import RELATIONSHIP_TYPE as RT ; part = doc.part ; r_id = part.relate_to(url, RT.HYPERLINK, is_external=True)Python Zipfile 實戰:Write vs Writestr —— 硬碟搬運工與記憶體魔術師; XML 處理的世界裡,writestr 才是真正的主角; zf.writestr(zinfo_or_arcname, data) #zinfo_or_arcname: ZipInfo | str ; data: bytes | str
近期留言