加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
read.py 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
bgArray 提交于 2022-04-03 20:42 . 存档
from xml.etree.ElementTree import *
import xmltodict
tree = ElementTree(file='./resources/测试精华(这是标题)1.mscx')
root = tree.getroot()
print(root.tag)
print(root.attrib)
print(type(root))
# for child in root:
# print(child.tag, child.attrib)
for ele in tree.iter():
# if ele.tag == "metaTag":
print(ele.tag, ele.attrib, ele.text)
# print("self." + ele.attrib.get("name") + " = [" + str(ele.attrib) + ", None]")
if __name__ == '__main__':
pass
# with open('./resources/测试精华(这是标题)1.mscx') as f:
# k = f.read()
# print(xml_to_dict(k))
# xmltodict.parse('./resources/测试精华(这是标题)1.mscx')
# with open('./resources/新建 文本文档.txt')as a:
# print(a.read())
with open('./resources/测试精华(这是标题)1.mscx', encoding='utf-8') as fd:
doc = xmltodict.parse(fd.read())
print(doc)
# print(doc['mydocument']['@has']) # == u'an attribute'
# print(doc['mydocument']['and']['many']) # == [u'elements', u'more elements']
# print(doc['mydocument']['plus']['@a']) # == u'complex'
# print(doc['mydocument']['plus']['#text']) # == u'element as well'
with open('./resources/新建 文本文档.txt', 'w', encoding='utf-8') as fd:
fd.write(str(doc))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化