def dt_docxml_to_text(self, filename): texts = "" document = zipfile.ZipFile(filename) xml_content = document.read('word/document.xml') root = ET(xml_content) sections = [] for section in root.getiterator(self.PARA): texts = '' for node in section.getiterator(self.TEXT): if node.text: texts += node.text sections.append(''.join(texts)) texts = '\n\n'.join(sections) document.close() return texts
def create_node(tag, property_map, content): '''新造一个节点 tag:节点标签 property_map:属性及属性值map content: 节点闭合标签里的文本内容 return 新节点''' element = ET(tag, property_map) element.text = content return element
def csc_to_graph(name): tree = ET(name) mote_id = [ int(t.text) for t in tree.findall(".//mote/interface_config/id") ] mote_type = [t.text for t in tree.findall(".//mote/motetype_identifier")] x = [float(t.text) for t in tree.findall(".//mote/interface_config/x")] y = [float(t.text) for t in tree.findall(".//mote/interface_config/y")] z = [float(t.text) for t in tree.findall(".//mote/interface_config/z")]
def fromstring(cls, string): root = ET.fromstring(string) tree = ET(element=root) return cls(tree)
def input_information(): for node in note.getiterator('student'): node = Element("name") node.text = input("이름을 입력하세요(종료는 'Enter' 입력): ") node.append(node) ET().write('students_info2.xml')
import xmltodict, json import requests import xml.etree.ElementTree as ET url = "http://www.colourlovers.com/api/palettes/top" r = requests.get(url) root = ET(r.content) print("results: ", root.attrib["numResults"]) for palette in root.iter("palette"): pass # o = xmltodict.parse('<e> <a>text</a> <a>text</a> </e>') # with open('jsonfiles/palettes/top.json', 'w') as outfile: # json.dump(r.content,outfile)