def test_fuel_tanks_content(self): script_path = self.sandbox.path_to_jsbsim_file('scripts', 'J2460.xml') fdm = CreateFDM(self.sandbox) fdm.load_script(script_path) fdm.run_ic() tree = et.parse(script_path) use_tag = tree.getroot().find('use') aircraft_name = use_tag.attrib['aircraft'] aircraft_path = self.sandbox.path_to_jsbsim_file('aircraft', aircraft_name) aircraft_tree = et.parse(os.path.join(aircraft_path, aircraft_name+'.xml')) total_fuel_quantity = 0.0 total_oxidizer_quantity = 0.0 for tank in aircraft_tree.findall('propulsion/tank'): contents = float(tank.find('contents').text) if tank.attrib['type'] == "FUEL": total_fuel_quantity += contents elif tank.attrib['type'] == 'OXIDIZER': total_oxidizer_quantity += contents self.assertAlmostEqual(fdm['propulsion/total-fuel-lbs'], total_fuel_quantity) self.assertAlmostEqual(fdm['propulsion/total-oxidizer-lbs'], total_oxidizer_quantity)
def redefine_app_name(): print "\n 新名字: hkc_app_name" try: from xml.etree import ElementTree as ET ET.register_namespace('android', xmlns_android); target_parse = ET.parse(target_file_path+"/AndroidManifest.xml") target_root = target_parse.getroot() target_application = target_root.find("application") orig_name = target_application.attrib[xmlns_android_attrib+"label"] print "orig_name:\t"+orig_name target_application.attrib[xmlns_android_attrib+"label"] = "@string/hkc_app_name" target_parse.write(target_file_path+"/AndroidManifest.xml","utf-8",True) target_strings_parse = ET.parse(target_file_path+"/res/values/strings.xml") target_string_root = target_strings_parse.getroot() strings_item = target_string_root.findall("string") for string_i in strings_item: if orig_name.find(string_i.attrib['name'])==8 : print string_i.text string_i.text = "@string/hkc_app_name" target_strings_parse.write(target_file_path+"/res/values/strings.xml","utf-8",True) except Exception, e: print "redefine_app_name "+str(e)
def isIgnored(myMedia): ignored = False msg = u'File: {0}'.format(myMedia.oStatus.fileName) Debug(msg) pchtrakt.logger.info(msg) ignored = isKeywordIgnored(myMedia.oStatus.fileName) if not ignored and ignored_repertory[0] != '': for el in myMedia.oStatus.fullPath.split('/'): if el <> '' and el in ignored_repertory: msg = 'This video is in a ignored repertory: {0}'.format(el) ignored = True break if not ignored and YamjIgnoredCategory[0] != '': if isinstance(myMedia.parsedInfo, mp.MediaParserResultTVShow): files = listdir(YamjPath) for file in files: if file.endswith('xml'): file = unicode(file, errors='replace') if file.find(myMedia.parsedInfo.name) >= 0: oXml = ElementTree.parse(YamjPath + file) ignored = isGenreIgnored(oXml.findall('.//genre')) if ignored: break else: file = unicode(myMedia.oStatus.fileName.rsplit('.',1)[0] + '.xml', errors='replace') oXml = ElementTree.parse(YamjPath + file) genres = oXml.findall('.//genre') ignored = isGenreIgnored(genres) return ignored
def multicopy(): try: tree = ET.parse('filecopylocations.xml') xroot = tree.getroot() print "\nWould you like to edit the following copy desitinations?\n" ET.dump(xroot) edit = raw_input("\ny=yes : n=no\n") if edit == 'y': initMulticopy() else: pass except: initMulticopy() tree = ET.parse('filecopylocations.xml') xroot = tree.getroot() print "\nPlease select the file you wish to have copied." try: fcpyfrom = getFileLocation() fcpyname = getFileNameFromFilePath(fcpyfrom) fcpyfrom = open(fcpyfrom, 'r').read() for loc in xroot.findall('Location'): f = open(loc.text + '/' + fcpyname, 'w') f.write(fcpyfrom) f.close() print "\nFile was successfully copied!" except: print "\nCould not copy file!" pass
def testExecuteStream(self): q = sesame.SesameQuery() q.names = ["ngc4258", "goob's star", "m51"] strm = q.execute_stream() doc = ET.parse(strm) strm.close() self.assertEquals("Sesame", doc.getroot().tag) strm = q.execute_stream(astext=True) doc = ET.parse(strm) strm.close() self.assertEquals("Sesame", doc.getroot().tag) strm = q.execute_stream(format="pc") lines = [] for line in strm: lines.append(line) strm.close() self.assertTrue(lines[0].startswith(b'#'), b"unexpected first line: " + lines[0]) self.assertTrue(lines[5].startswith(b'%'), b"unexpected mid-line: " + lines[5]) q.dbs = "GB" self.assertRaises(sesame.DALQueryError, q.execute_stream) strm = q.execute_stream(lax=True) strm.close() del q.dbs self.assertEquals("", q.dbs) q.opts = "bo" self.assertRaises(sesame.DALQueryError, q.execute_stream) strm = q.execute_stream(lax=True) strm.close()
def setUp(self): JSBSimTestCase.setUp(self) self.fdm = CreateFDM(self.sandbox) self.script_path = self.sandbox.path_to_jsbsim_file('scripts', 'c1722.xml') # Read the time step 'dt' from the script file self.tree = et.parse(self.script_path) root = self.tree.getroot() use_tag = root.find('use') aircraft_name = use_tag.attrib['aircraft'] self.run_tag = root.find('run') self.dt = float(self.run_tag.attrib['dt']) # Read the date at which the trim will be run for event in root.findall('run/event'): if event.attrib['name'] == 'Trim': cond_tag = event.find('condition') self.trim_date = float(cond_tag.text.split()[-1]) break # Read the output rate and the output file from the aircraft file aircraft_path = self.sandbox.path_to_jsbsim_file('aircraft', aircraft_name, append_xml(aircraft_name)) tree = et.parse(aircraft_path) output_tag = tree.getroot().find('output') self.output_file = output_tag.attrib['name'] self.rateHz = float(output_tag.attrib['rate']) self.rate = int(1.0 / (self.rateHz * self.dt))
def globalVars(): global _home_dir, _desktop_folder, _script_dir, xml_cache_root, xml_options_root, _cache_dir, _options_dir, qualityPresets _home_dir = QtCore.QDir.homePath() _desktop_folder = _home_dir + "/Desktop/" _script_dir = QtCore.QDir.currentPath() _cache_dir = _script_dir + "/cache.xml" _options_dir = _script_dir + "/options.xml" external_dir = _home_dir + "/.audiobookEncoder" if not os.path.exists(external_dir): os.makedirs(external_dir) if not os.path.exists(external_dir + "/cache.xml"): shutil.copy2(_cache_dir, external_dir) if not os.path.exists(external_dir + "/options.xml"): shutil.copy2(_options_dir, external_dir) _cache_dir = external_dir + "/cache.xml" _options_dir = external_dir + "/options.xml" # xml table auslesen xml_cache_root = ET.parse(_cache_dir).getroot() xml_options_root = ET.parse(_options_dir).getroot() qualityPresets = ["96 Kbps, Stereo, 48 kHz", "128 Kbps, Stereo, 48 kHz", "256 Kbps, Stereo, 48 kHz", "320 Kbps, Stereo, 48 kHz"]
def intiMulticopy(self): fio = fIO() fio.filePrompt("folder", "Please select folder locations to copy to.\n" + "These folder locations will be saved.\n" + "When you are done loading in locations, simply\n" + "press the cancel button in the file explorer.\n") try: tree = ET.parse('filecopylocations.xml') xroot = tree.getroot() for locs in xroot.findall('Location'): xroot.remove(locs) except: froot = ET.Element('Directories') tree = ET.ElementTree(froot) tree.write('filecopylocations.xml') tree = ET.parse('filecopylocations.xml') xroot = tree.getroot() locnum = 1 fio.folderLocation() floc = fio.getFolderLocation() while(floc != ''): try: loc = ET.SubElement(xroot, 'Location'.format(locnum)) loc.set('index', '{0}'.format(locnum)) locnum = locnum + 1 loc.text = floc floc = fio.getFolderLocation() except: floc = '' tree.write('filecopylocations.xml') ET.dump(xroot)
def main(): parser = argparse.ArgumentParser(description="Compare two flattened UW XML files.") parser.add_argument("xml_file_1", help="The first XML file.") parser.add_argument("xml_file_2", help="The second XML file.") args = parser.parse_args() if not os.path.isfile(args.xml_file_1): sys.exit("ERROR - Can't find first XML file: {}".format(args.xml_file_1)) if not os.path.isfile(args.xml_file_2): sys.exit("ERROR - Can't find second XML file: {}".format(args.xml_file_2)) tree1 = ElementTree.parse(args.xml_file_1) tree2 = ElementTree.parse(args.xml_file_2) dict1 = _elementToDict(tree1.getroot()) dict2 = _elementToDict(tree2.getroot()) diff = dict_diff(dict1, dict2) if(len(diff)): print "Differences:" for key, value in diff.items(): first = str(value[0]).split('),') second = str(value[1]).split('),') result = list(difflib.context_diff(first, second, lineterm="")) pprint(result) else: print "No Differences"
def get_channels(name,url): from xml.etree import ElementTree if url.startswith('http://'): source = get_page_source(url) else: source = readfile(url) save(os.path.join(pastaperfil,"working.xml"),source) chlist_tree = ElementTree.parse(os.path.join(pastaperfil,"working.xml")) LANGUAGE = "en" groups = ElementTree.parse(os.path.join(pastaperfil,"working.xml")).findall('.//group') for group in groups: if group.attrib[LANGUAGE].encode('utf-8') == name: channels = group.findall('.//channel') for channel in channels: try: title = channel.find('.//name').attrib['en'].encode('utf-8') if not title: title = channel.find('.//name').attrib['cn'].encode('utf-8') tipo = channel.find('.//stream_type').text sop_address = channel.find('.//item').text if not tipo: tipo = "N/A" if not title: title = "N/A" thumbnail = "" try: thumbnail = channel.find('.//thumbnail').text except: pass if sop_address: if thumbnail == "": thumbnail = addonpath + art + 'sopcast_link.png' try: addDir_livestreams_common('[B][COLOR orange]' + title + ' [/B][/COLOR](' + tipo +')',sop_address,2,thumbnail,False) except:pass else: pass except: pass else: pass
def get_languages(self): # try and find language files (Kodi 14.x) try: return [folder for folder in os.listdir('/usr/share/kodi/language/')] except: pass # if we have not found yet try looking for laonguage addons (Kodi 15.x) languages = ['English'] languagedirs = ["/home/osmc/.kodi/addons", "/usr/share/kodi/addons" ] language_folder_contents = [] try: log("Contents of %s: %s" % (languagedirs[0], os.listdir(languagedirs[0]))) language_folder_contents = os.listdir(languagedirs[0]) except: log('%s not found' % languagedirs[0]) try: log("Contents of %s: %s" % (languagedirs[1], os.listdir(languagedirs[1]))) language_folder_contents = os.listdir(languagedirs[1]) except: log('%s not found' % languagedirs[1]) for folder in language_folder_contents: if folder.startswith('resource.language.'): try: tree = ET.parse('/home/osmc/.kodi/addons/' + folder + os.sep + 'addon.xml') except: tree = ET.parse('/usr/share/kodi/addons/' + folder + os.sep + 'addon.xml') root = tree.getroot() log('Name from %s: %s' % (folder, root.attrib['name'])) languages.append(root.attrib['name']) return languages
def getXML(filepath): fext = os.path.splitext(filepath)[1] if fext == '.gz': tmp = GzipFile(filename=filepath) return ET.parse(tmp) else: return ET.parse(filepath)
def parse_configs( self ): self.config = load_app_properties( ini_file=self.config_file ) job_conf_xml = self.config.get( "job_config_file", join( dirname( self.config_file ), 'job_conf.xml' ) ) try: for plugin in ElementTree.parse( job_conf_xml ).find( 'plugins' ).findall( 'plugin' ): if 'load' in plugin.attrib: self.job_runners.append( plugin.attrib['load'] ) except (OSError, IOError): pass object_store_conf_xml = self.config.get( "object_store_config_file", join( dirname( self.config_file ), 'object_store_conf.xml' ) ) try: for store in ElementTree.parse( object_store_conf_xml ).iter( 'object_store' ): if 'type' in store.attrib: self.object_stores.append( store.attrib['type'] ) except (OSError, IOError): pass # Parse auth conf auth_conf_xml = self.config.get( "auth_config_file", join( dirname( self.config_file ), 'auth_conf.xml' ) ) try: for auth in ElementTree.parse( auth_conf_xml ).findall( 'authenticator' ): auth_type = auth.find('type') if auth_type is not None: self.authenticators.append( auth_type.text ) except (OSError, IOError): pass
def test_CAS_ic(self): script_name = 'Short_S23_3.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) # Add a Pitot angle to the Short S23 tree, aircraft_name, path_to_jsbsim_aircrafts = CopyAircraftDef(script_path, self.sandbox) self.addPitotTube(tree.getroot(), 5.0) tree.write(self.sandbox('aircraft', aircraft_name, aircraft_name+'.xml')) # Read the CAS specified in the IC file tree = et.parse(script_path) use_element = tree.getroot().find('use') IC_file = use_element.attrib['initialize'] tree = et.parse(os.path.join(path_to_jsbsim_aircrafts, append_xml(IC_file))) vc_tag = tree.getroot().find('./vc') VCAS = float(vc_tag.text) if 'unit' in vc_tag.attrib and vc_tag.attrib['unit'] == 'FT/SEC': VCAS /= 1.68781 # Converts in kts # Run the IC and check that the model is initialized correctly fdm = CreateFDM(self.sandbox) fdm.set_aircraft_path('aircraft') fdm.load_script(script_path) fdm.run_ic() self.assertAlmostEqual(fdm['ic/vc-kts'], VCAS, delta=1E-7) self.assertAlmostEqual(fdm['velocities/vc-kts'], VCAS, delta=1E-7)
def test_write_keyword_in_iso_metadata(self): keyword_file = test_data_path('other', 'expected_multilayer.keywords') with open(keyword_file) as f: keywords = f.read() basename, _ = os.path.splitext(keyword_file) xml_file = basename + '.xml' if os.path.isfile(xml_file): os.remove(xml_file) # there should be no xml file now self.assertFalse( os.path.isfile(xml_file), 'File %s should not exist' % xml_file) xml_file = write_keyword_in_iso_metadata(keyword_file) tree = ElementTree.parse(xml_file) root = tree.getroot() keyword_tag = root.find(ISO_METADATA_KEYWORD_TAG) self.assertIn(keywords, keyword_tag.text) # there should be an xml file now self.assertTrue( os.path.isfile(xml_file), 'File %s should exist' % xml_file) # lets update the file xml_file = write_keyword_in_iso_metadata(keyword_file) tree = ElementTree.parse(xml_file) keyword_tag = tree.getroot().find(ISO_METADATA_KEYWORD_TAG) self.assertIn(keywords, keyword_tag.text) os.remove(xml_file)
def readFile(filename): """ Open svg file and return root xml object """ GZIPMAGIC = '\037\213' try: fh = open(filename, 'rb') except IOError: raise SVGError("could not open file '%s' for reading" % filename) # test for gzip compression magic = fh.read(2) fh.close() if magic == GZIPMAGIC: svg = etree.parse(GzipFile(filename, 'r')) else: svg = etree.parse(filename) root = svg.getroot() if not root.tag == Renderer.SVG_ROOT: raise SVGError("Expected SVG fragment as root object") return root
def __init__(self, library_name, version = None): self._library_name = library_name # Get a list of possible locations the library can be found at search_path = self._get_library_search_path(version) for path in search_path: # Append the name of the library and the .lbr extension path += library_name + '.lbr' try: print('Opening library file: ' + path) if path[0:4] != 'http': eagle_node = ElementTree.parse(path).getroot() # <eagle> else: try: eagle_node = ElementTree.parse(urlopen(path)).getroot() except: continue # Store the <library> node so that we can access it later self._library_node = eagle_node.find('drawing').find('library') break # Success, return except IOError: # Library file doesn't exist, move on to the next one continue else: # Ran out of library files to try raise Exception('Could not open library file')
def submit_ID(request): if request.method == 'POST': # If the form has been submitted... form = AccountForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Get steam64ID steamID = form.cleaned_data['steamID'] data = urllib2.urlopen("http://steamcommunity.com/id/%s?xml=1" % steamID) tree = ElementTree.parse(data) steam64ID = tree.find("steamID64").text # Get achievements data = urllib2.urlopen( "http://steamcommunity.com/profiles/%s/stats/%s/?xml=1" % (steam64ID, "TF2")) tree = ElementTree.parse(data) achievements = [] for achievement in tree.getroot().find("achievements").findall( "achievement"): if not achievement.find("unlockTimestamp") == None: achievements.append(achievement.find("name").text) print achievements return HttpResponseRedirect('/book/display_achievements') # Redirect after POST else: form = AccountForm() # An unbound form return render_to_response('test_achievements.html', { 'form': form, }, context_instance=RequestContext(request))
def buttonevent_preview(self, event): print('debug: Preview...') self.options_generation() #system_preview = copy.deepcopy(self.system) tree = ET.parse(self.options.config_file) root = tree.getroot() system_preview = chris.System(root.attrib['name'], []) allnode, alllb, allimage = get_nodes.get_nodes(self.options.key_file, get_keys.get_keys(True, self.options.key_file)) system_preview.reconfig(root, False, allnode, self.options, alllb) #system_preview.name = 'temp' if not reconfig_single.reconfig_single(self.options, system_preview, self.allnode, self.alllb, modifyxml = False): print('Error; reconfig_single()') return tree = ET.parse('configuration.preview.xml') root = tree.getroot() #print('debug: finish parsing') system_preview = chris.System(root.attrib['name'], []) if not system_preview.reconfig(root, False, allnode, self.options, alllb, preview = True): print('Error: Preview Failed') return # #print('Warning: This faile might already cause some changes to the system') # exit() print('hi') self.tree_generation(self.m_treeCtrl2, system_preview = system_preview) self.m_treeCtrl2.ExpandAll() self.m_staticText11.SetLabel("After Reconfig (Estimated)") self.m_treeCtrl2.Update()
def parse_xml(file_name): """Returns a parsed xml tree with comments intact.""" error_message = '' if not os.path.exists(file_name): return None, "File does not exist %s" % str(file_name) fobj = open(file_name, 'r') if using_python_27: try: tree = XmlET.parse(fobj, parser=XmlET.XMLParser(target=Py27CommentedTreeBuilder())) except Exception as e: fobj.close() error_message = "Exception attempting to parse %s: %s" % (str(file_name), str(e)) log.exception(error_message) return None, error_message else: try: tree = XmlET.parse(fobj, parser=Py26CommentedTreeBuilder()) except Exception as e: fobj.close() error_message = "Exception attempting to parse %s: %s" % (str(file_name), str(e)) log.exception(error_message) return None, error_message fobj.close() return tree, error_message
def buildMedDict(): medDict = dict() dictTree = ET.parse('dictionary_files/radlex_xml_synonyms.xml') root = dictTree.getroot() print("Loaded radlex") for lexRecord in root.findall('lexRecord'): mapping = lexRecord.find('base').text.lower() for word in lexRecord.findall('inflVars'): medDict[word.text.lower()] = mapping print("Added radlex") dictTree = ET.parse('dictionary_files/LEXICON.xml') root = dictTree.getroot() print("Loaded SPECIALIST") for lexRecord in root.findall('lexRecord'): mapping = lexRecord.find('base').text.lower() for word in lexRecord.findall('inflVars'): medDict[word.text.lower()] = mapping for word in lexRecord.findall('acronyms'): medDict[word.text.lower()] = mapping print("Added Specialist") file = open('./dictionary_files/medical.pkl', 'w') pickle.dump(medDict, file) file.close() print("done") print(medDict["aneurysm of ascending aorta"])
def getxmlway(self, eID, maxDepth): runnames = [] tree = xml.parse(self.main_path) dom = tree.getroot() depth = 0 revDom = reversed(dom) print "Searching Journals for RB number: " + str(eID) for cycle in revDom: journal_file = cycle.attrib.get('name') journal_path = os.path.join(self.base_path, journal_file) cycle_id = self.__findCycleId(journal_path) if cycle_id: cycle = 'cycle_'+ cycle_id cycle_dir_path = os.path.join(self.instr_path, 'data', cycle) # side effect. self.__addSettingDirToManagedUserDirs(cycle_dir_path) cycletree = xml.parse(journal_path) root = cycletree.getroot() for run in root: runno = None title = None if (run[4].text == eID): runno = run[6].text.strip() title = run[0].text.strip() if title and runno: journalentry = runno + ": " + title runnames.append(journalentry) depth = depth + 1 if depth >= maxDepth: break else: print "Error reading journal: " + journal_path + " - could not identify associated cycle. Skipping journal." print "Search for RB number " + str(eID) + " complete. Search yielded " + str(len(runnames)) + " results" return runnames
def getServers(self): xmlSvrRoot = ET.parse(self.serverXML).getroot() xmlUplRoot = ET.parse(self.uplinkXML).getroot() servers = [] for svr in xmlSvrRoot.iter(XMLServerDAO.SERVER): # Let's dynamically load the class definition svrClassName = svr.find(Server.SERVER_CLASS).text svrClass = self.getServerClass(svrClassName) # Retrieve the server information args = [] for attr in Server.SERVER_ARGS: args.append(svr.find(attr).text) # Retrieve the uplink information uplink = svr.find(Server.SERVER_UPLINK).text for upl in xmlUplRoot.iter(Server.SERVER_UPLINK): if (upl.find(Server.SERVER_UPLINK_NAME).text == uplink): for attr in Server.UPLINK_ARGS: args.append(upl.find(attr).text) break # Retrieve extra information related to the server class type for attr in svrClass.SERVER_ARGS: args.append(svr.find(attr).text) # Add the server to the list of servers servers.append(svrClass(*args)) return servers
def test_subreadset_metadata_element_name(self): # without touching the element: sset = SubreadSet(data.getXml(10)) log.debug(data.getXml(10)) fn = tempfile.NamedTemporaryFile(suffix=".subreadset.xml").name log.debug(fn) sset.write(fn) f = ET.parse(fn) self.assertEqual(len(f.getroot().findall( '{http://pacificbiosciences.com/PacBioDatasets.xsd}' 'SubreadSetMetadata')), 0) self.assertEqual(len(f.getroot().findall( '{http://pacificbiosciences.com/PacBioDatasets.xsd}' 'DataSetMetadata')), 1) # with touching the element: sset = SubreadSet(data.getXml(10)) sset.metadata.description = 'foo' fn = tempfile.NamedTemporaryFile(suffix=".subreadset.xml").name sset.write(fn, validate=False) f = ET.parse(fn) self.assertEqual(len(f.getroot().findall( '{http://pacificbiosciences.com/PacBioDatasets.xsd}' 'SubreadSetMetadata')), 0) self.assertEqual(len(f.getroot().findall( '{http://pacificbiosciences.com/PacBioDatasets.xsd}' 'DataSetMetadata')), 1)
def readProjectFiles(psxFilePath): filesFilePath = psxFilePath.rsplit('.',1)[0]+'.files' os.chdir(filesFilePath) chunkZipPath = filesFilePath+'/0/chunk.zip' frameZipPath = filesFilePath+'/0/0/frame.zip' modelZipPath = filesFilePath+'/0/0/model/model.zip' thumbnailsZipPath = filesFilePath+'/0/0/thumbnails/thumbnails.zip' pointcloudZipPath = filesFilePath+'/0/0/point_cloud.1/point_cloud.zip' chunkZip = ZipFile(chunkZipPath) chunkZip.extractall() shutil.move('doc.xml', 'chunk.xml') frameZip = ZipFile(frameZipPath) frameZip.extractall() shutil.move('doc.xml', 'frame.xml') modelZip = ZipFile(modelZipPath) modelZip.extractall() modelPath = filesFilePath+'/'+'mesh.ply' chunktree = ET.parse('chunk.xml') chunkroot = chunktree.getroot() chunkcameras = chunkroot.find('cameras') frametree = ET.parse('frame.xml') frameroot = frametree.getroot() framecameras = frameroot.find('cameras') return chunkroot, frameroot, modelPath
def main(toc_path, output_path): tree = ET.parse(join(toc_path, "TOC.xml")) root = tree.getroot() #output file if not exists(output_path): makedirs(output_path) filePath = join(output_path, root.attrib['RELEASE_NO']) with open(filePath, 'w+') as output_file: publication = root.attrib["PUBLICATION"] print root.attrib Meta = root.findall("./Head_np/Meta") publication_date= Meta[0].attrib["BASE_HREF"] sections = root.findall("./Body_np/Section") u = [("publication_date","entity_id")] for section in sections: pages = section.findall('Page') for page in pages: href = page.attrib['HREF'] page_path = join(toc_path, href[1:] + '.xml') page_xml = ET.parse(page_path) entities = page_xml.findall('Entity') for entity in entities: entity_ID = entity.attrib['ID'] entity_path = join(publication_date, entity_ID) entity_tuple=(publication_date,entity_ID) u.append(entity_tuple) print u
def merge_androidmanifest_xml(): try: from xml.etree import ElementTree as ET ET.register_namespace('android', xmlns_android); #extra项目 print extra_file_path+"/AndroidManifest.xml" extra_parse = ET.parse(extra_file_path+"/AndroidManifest.xml") extra_root = extra_parse.getroot() #target项目 target_parse = ET.parse(target_file_path+"/AndroidManifest.xml") target_root = target_parse.getroot() target_application = target_root.find("application") #合并两个项目的meta-data merge_hkc_meta(extra_root,target_root,target_application) #合并两个项目的activity merge_hkc_activity(extra_root,target_root,target_application) #合并两个项目的service merge_hkc_services(extra_root,target_root,target_application) target_parse.write(target_file_path+"/AndroidManifest.xml","utf-8",True) except Exception, e: print "merge_androidmanifest_xml "+str(e)
def setLayerStyle(self, layerWorkspace, dataStoreName, layerName, styleWorkspace, styleName): """ Set the default style of given layer. Set styleWorkspace to None to refer to styles with no workspace. """ # Open the style XML stylePath = self.getStyleXmlPath(styleWorkspace, styleName) styleTree = Xml.parse(stylePath) styleRoot = styleTree.getroot() logging.debug("[GsXml][setLayerStyle] Assigning style '%s'"% stylePath) # Get the style id styleId = styleRoot.find("./id").text #<style><id> logging.debug("[GsXml][setLayerStyle] with <Id> '%s'"% styleId) # Open the layer XML layerPath = self.getLayerPath(layerWorkspace, dataStoreName, layerName) layerTree = Xml.parse(layerPath) layerRoot = layerTree.getroot() logging.debug("[GsXml][setLayerStyle] to layer '%s'"% layerPath) # Change the default style layerRoot.find("./defaultStyle/id").text = styleId #<layer><defaultStyle<id> # Write the layer file layerTree.write(layerPath) logging.debug("[GsXml][setLayerStyle] Style assigned")
def ModRowWith(self, withAttrs, withValues, inThisTable, thatAttrs, thatValues): dataToModify = self.GetRowWith(withAttrs, withValues, inThisTable) nameToLocation = {} index = 0 self.DelRowWith(withAttrs, withValues, inThisTable) structure = XML.parse(self.brain.GetTableFileName(inThisTable, self.use)).getroot() for field in XML.parse(self.brain.GetTableFileName(inThisTable, self.use)).getroot()[1]: nameToLocation[field.attrib["Name"]] = index index += 1 for i in range(len(thatAttrs)): for k in range(len(dataToModify)): dataToModify[k][nameToLocation[thatAttrs[i]]] = thatValues[i] for data in dataToModify: entryTag = XML.Element("Entry") for datum in data: dTag = XML.Element("D") dTag.text = datum entryTag.append(dTag) structure[2].append(entryTag) with open(self.brain.GetTableFileName(inThisTable, self.use), "w") as outputFile: XML.ElementTree(structure).write(outputFile)
def populate_store(self, store): """ @description: load the treeview from the google2ubuntu.xml file or from the default.xml file @param: store the listStore that will be modify """ config = expanduser('~') +'/.config/google2ubuntu/google2ubuntu.xml' path = os.path.dirname(os.path.abspath(__file__)).strip('librairy') lang = locale.getlocale()[0] if os.path.isdir(path+'config/'+lang) == False: lang='en_EN' default = path +'config/'+lang+'/default.xml' try: if os.path.isfile(config): tree = ET.parse(config) else: if os.path.exists(expanduser('~') +'/.config/google2ubuntu') == False: os.makedirs(expanduser('~') +'/.config/google2ubuntu') os.system('cp -r /usr/share/google2ubuntu/modules '+expanduser('~') +'/.config/google2ubuntu') tree = ET.parse(default) root = tree.getroot() for entry in root.findall('entry'): Type=entry.get('name') Key=entry.find('key').text Command=entry.find('command').text store.append([Key, Command, Type]) except Exception: print 'Error while reading config file'
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, title TEXT UNIQUE, album_id INTEGER, genre_id INTEGER, len INTEGER, rating INTEGER, count INTEGER )''') mycur.execute('''DELETE FROM Artist;''') mycur.execute('''DELETE FROM Genre;''') mycur.execute('''DELETE FROM Album;''') mycur.execute('''DELETE FROM Track;''') #fname = raw_input("Enter XML Library file: ") #if ( len(fname) < 1 ) : fname = "tracks/Library.xml" myxml = ET.parse(fname) def getData(haystack, needle): found = False for inTag in haystack: if found == True: return inTag.text if inTag.tag == 'key' and inTag.text == needle: found=True return None dictData = myxml.findall('dict/dict/dict') for entry in dictData: if getData(entry, 'Track ID') is None: continue name = getData(entry, 'Name')
def dicts_equal(dict1, dict2): for key in dict1: if key not in dict2: return False if dict1[key] != dict2[key]: return False if len(dict1) != len(dict2): return False return True print('testing osm_helper.way_node_ids') test_count = 0 correct_tests = 0 failure = None element_tree = ET.parse('testdata/map.osm') root = element_tree.getroot() helper = osm_helper.OsmHelper(element_tree) with open('testdata/way_node_ids.out', 'r') as f: for element in root: if element.tag == 'way': fail = None result = helper.way_node_ids(element) expected = f.readline().split() if not isinstance(result, list): fail = 'expected list, got {}'.format(type(result)) elif result != expected: fail = 'expected \n{},\n got \n{}\n'.format(expected, result) else:
) # key is (threadid,postid), value is textual content of post rootdir = "../dataconversion/Viva_forum/samples/106long20threads" for f in os.listdir(rootdir): if f.endswith("xml"): postids = list() termvectors = dict( ) # key is postid, value is dict with term -> termcount for post termvectorforthread = dict( ) # key is term, value is termcount for full thread termvectorfortitle = dict( ) # key is term, value is termcount for title authorcountsinthread = dict( ) # key is authorid, value is number of posts by author in this thread tree = ET.parse(rootdir + "/" + f) root = tree.getroot() for thread in root: threadid = thread.get('id') category = thread.find('category').text title = thread.find('title').text for posts in thread.findall('posts'): noofposts = len(posts.findall('post')) if noofposts > 50: noofposts = 50 postcount = 0 #print time.clock(), "\t", "extract feats from each post"
def postprocess_translations(reduce_diff_hacks=False): print('Checking and postprocessing...') if reduce_diff_hacks: global _orig_escape_cdata _orig_escape_cdata = ET._escape_cdata ET._escape_cdata = escape_cdata for (filename, filepath) in all_ts_files(): os.rename(filepath, filepath + '.orig') have_errors = False for (filename, filepath) in all_ts_files('.orig'): # pre-fixups to cope with transifex output parser = ET.XMLParser( encoding='utf-8' ) # need to override encoding because 'utf8' is not understood only 'utf-8' with open(filepath + '.orig', 'rb') as f: data = f.read() # remove control characters; this must be done over the entire file otherwise the XML parser will fail data = remove_invalid_characters(data) tree = ET.parse(io.BytesIO(data), parser=parser) # iterate over all messages in file root = tree.getroot() for context in root.findall('context'): for message in context.findall('message'): numerus = message.get('numerus') == 'yes' source = message.find('source').text translation_node = message.find('translation') # pick all numerusforms if numerus: translations = [ i.text for i in translation_node.findall('numerusform') ] else: translations = [translation_node.text] for translation in translations: if translation is None: continue errors = [] valid = check_format_specifiers(source, translation, errors, numerus) for error in errors: print('%s: %s' % (filename, error)) if not valid: # set type to unfinished and clear string if invalid translation_node.clear() translation_node.set('type', 'unfinished') have_errors = True # Remove location tags for location in message.findall('location'): message.remove(location) # Remove entire message if it is an unfinished translation if translation_node.get('type') == 'unfinished': context.remove(message) # check if document is (virtually) empty, and remove it if so num_messages = 0 for context in root.findall('context'): for message in context.findall('message'): num_messages += 1 if num_messages < MIN_NUM_MESSAGES: print('Removing %s, as it contains only %i messages' % (filepath, num_messages)) continue # write fixed-up tree # if diff reduction requested, replace some XML to 'sanitize' to qt formatting if reduce_diff_hacks: out = io.BytesIO() tree.write(out, encoding='utf-8') out = out.getvalue() out = out.replace(b' />', b'/>') with open(filepath, 'wb') as f: f.write(out) else: tree.write(filepath, encoding='utf-8') return have_errors
import xml.etree.ElementTree as ET tree = ET.parse("lista1b.jff") automaton = tree.getroot() #le o estado, mas falta uma forma de procurar a tag 'initial' e 'final' for elem in automaton[1].iter('state'): for subchild in elem.iter('initial'): print(" ", elem.attrib) #pegar o id for subchild in elem.iter('final'): print(" ", elem.attrib) #pegar o id #for subchild in elem.iter('y'): #print("y ",subchild.text) print(elem.tag, elem.attrib) #le transicoes: (from, to, read) for elem in automaton[1].iter('transition'): print(elem[0].text, elem[1].text, elem[2].text)
#!/usr/bin/env python # -*- coding: utf-8 -*- # k54.py from xml.etree import ElementTree as ET if __name__ == '__main__': filename = "../data/nlp.txt.xml" root = ET.parse(filename) for token in root.iter('token'): word = token.findtext('word') lemma = token.findtext('lemma') pos = token.findtext('POS') print('\t'.join([word, lemma, pos]))
# -*- coding: utf-8 -*- """ Created on Thu Apr 30 13:32:29 2015 @author: 577342 """ import xml.etree.ElementTree as ET tree = ET.parse( 'Documents\\Data Science Projects\\Tale of 2 Cities\\Tale of Two Cities.kml' ) root = tree.getroot() root.tag root.attrib for child in root[0][4][2][2][1][0]: print child.tag, child.attrib, child.text for child in tree.findall('.//{http://www.opengis.net/kml/2.2}coordinates'): print child.tag, child.attrib, child.text for placemark in tree.findall('.//{http://www.opengis.net/kml/2.2}Placemark'): name = placemark.find('.//{http://www.opengis.net/kml/2.2}name') print name.text coordinates = placemark.find( './/{http://www.opengis.net/kml/2.2}coordinates') for coord in coordinates.text.strip().split(' '): print coord.split(',') kml_text = """<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"
import os import xml.etree.ElementTree as et base_path = os.path.dirname(os.path.realpath(__file__)) xml_file = os.path.join(base_path, "data\\product_listing.xml") tree = et.parse(xml_file) root = tree.getroot() # for child in root: # print(child.tag, child.attrib) # for child in root: # for element in child: # print(element.tag, ":", element.text) new_product = et.SubElement(root, "product", attrib={"id": "4"}) new_prod_name = et.SubElement(new_product, "name") new_prod_desc = et.SubElement(new_product, "description") new_prod_cost = et.SubElement(new_product, "cost") new_prod_ship = et.SubElement(new_product, "shipping") new_prod_name.text = "Python Pants" new_prod_desc.text = "These pants will surely help you code like crazy!" new_prod_cost.text = "39.95" new_prod_ship.text = "4.00" tree.write(xml_file)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import xml.etree.ElementTree as ET tree = ET.parse('Morphalou/commonNoun_Morphalou3.1_LMF.xml') root = tree.getroot() def get_lexical_entry(orthography): """ Takes the orthographic form of a noun, returns its xml entry in the morphalou file """ for element in tree.findall('./lexicalEntry'): for ortho in element.findall('./formSet/inflectedForm/orthography'): if ortho.text == orthography: return element raise ValueError('Lexical entry not found') def get_gender(orthography): """ Takes the orthographic form of a noun, returns its gender. possible values: masculine, feminine, invariant """ lexical_entry = get_lexical_entry(orthography) if lexical_entry is not None: return lexical_entry.find( './formSet/lemmatizedForm/grammaticalGender').text
import xml.etree.ElementTree as ET import pprint tree = ET.parse("exampleResearchArticle.xml") root = tree.getroot() print("\nChildren of root:") for child in root: print(child.tag) title = root.find('./fm/bibl/title') #support basic x path expression title_text = '' for p in title: title_text += p.text print("\nTitle:\n",title_text) print("\nAuthor email addresses:") for a in root.findall('./fm/bibl/aug/au'): email = a.find('email') if email is not None: print(email.text)
def convert_map(directory_path): map_files = [ f for f in listdir(directory_path) if isfile(join(directory_path, f)) ] for file in map_files: if file == 'bob.mp': continue with open(directory_path + '/' + file) as f: input_tree = ET.parse(f) input_root = input_tree.getroot() output_root = ET.Element('cereal') output_map = ET.Element('map') output_root.append(output_map) output_cereal_class = ET.Element('cereal_class_version') output_cereal_class.text = '0' output_map.append(output_cereal_class) output_value0 = ET.Element('value0') output_value0.text = '27' output_value1 = ET.Element('value1') output_value1.text = '21' output_map.append(output_value0) output_map.append(output_value1) for node in input_root: if node.tag == 'map': for subnode in node: if subnode.tag == 'm_name': output_name = ET.Element('value569') output_name.text = subnode.text output_map.append(output_name) if subnode.tag == 'm_players': value_player = 570 for player in subnode: if player.tag == 'item': player_pos = list(player) output_player = ET.Element( "value{}".format(value_player)) if value_player == 570: player_cereal_class_version = ET.Element( 'cereal_class_version') player_cereal_class_version.text = '0' output_player.append( player_cereal_class_version) output_pos_x = ET.Element('value0') output_pos_x.text = player_pos[0].text output_player.append(output_pos_x) output_pos_y = ET.Element('value1') output_pos_y.text = player_pos[1].text output_player.append(output_pos_y) output_map.append(output_player) value_player += 1 if subnode.tag == 'm_array': for tag in subnode: if tag.tag == 'data': value_array = 2 for item in tag: item_values = list(item) output_item = ET.Element( "value{}".format(value_array)) if value_array == 2: item_cereal_class_version = ET.Element( 'cereal_class_version') item_cereal_class_version.text = '0' output_item.append( item_cereal_class_version) pos_x = ET.Element("value0") pos_x.text = item_values[0].text output_item.append(pos_x) pos_y = ET.Element("value1") pos_y.text = item_values[1].text output_item.append(pos_y) properties = ET.Element("value2") properties.text = item_values[2].text output_item.append(properties) tile_type = ET.Element("value3") tile_type.text = item_values[3].text output_item.append(tile_type) output_map.append(output_item) value_array += 1 output_tree = ET.ElementTree(output_root) with open(directory_path + '/' + file, 'wb') as f: output_tree.write(f, encoding='utf-8', xml_declaration=True)
def test_empty_file_is_skipped(self): cov = self.run_doit() cov.xml_report(skip_empty=True) dom = ElementTree.parse("coverage.xml") elts = dom.findall(".//class[@name='__init__.py']") assert len(elts) == 0
def solvedepsAndroid(self): installPrefix = self.qmakeQuery(var='QT_INSTALL_PREFIX') qtLibsPath = self.qmakeQuery(var='QT_INSTALL_LIBS') jars = [] permissions = set() features = set() initClasses = set() libs = set() for f in os.listdir(self.libInstallDir): basename = os.path.basename(f)[3:] basename = os.path.splitext(basename)[0] depFile = os.path.join(qtLibsPath, basename + '-android-dependencies.xml') if os.path.exists(depFile): tree = ET.parse(depFile) root = tree.getroot() for jar in root.iter('jar'): jars.append(jar.attrib['file']) if 'initClass' in jar.attrib: initClasses.append(jar.attrib['initClass']) for permission in root.iter('permission'): permissions.add(permission.attrib['name']) for feature in root.iter('feature'): features.add(feature.attrib['name']) for lib in root.iter('lib'): if 'file' in lib.attrib: libs.add(lib.attrib['file']) print('Copying jar files\n') for jar in sorted(jars): srcPath = os.path.join(installPrefix, jar) dstPath = os.path.join(self.rootInstallDir, 'libs', os.path.basename(jar)) print(' {} -> {}'.format(srcPath, dstPath)) self.copy(srcPath, dstPath) manifest = os.path.join(self.rootInstallDir, 'AndroidManifest.xml') manifestTemp = os.path.join(self.rootInstallDir, 'AndroidManifestTemp.xml') tree = ET.parse(manifest) root = tree.getroot() oldFeatures = set() oldPermissions = set() for element in root: if element.tag == 'uses-feature': for key in element.attrib: if key.endswith('name'): oldFeatures.add(element.attrib[key]) elif element.tag == 'uses-permission': for key in element.attrib: if key.endswith('name'): oldPermissions.add(element.attrib[key]) features -= oldFeatures permissions -= oldPermissions featuresWritten = len(features) < 1 permissionsWritten = len(permissions) < 1 replace = { '-- %%INSERT_INIT_CLASSES%% --': ':'.join(sorted(initClasses)), '-- %%BUNDLE_LOCAL_QT_LIBS%% --': '1', '-- %%USE_LOCAL_QT_LIBS%% --': '1', '-- %%INSERT_LOCAL_LIBS%% --': ':'.join(sorted(libs)), '-- %%INSERT_LOCAL_JARS%% --': ':'.join(sorted(jars)) } with open(manifest) as inFile: with open(manifestTemp, 'w') as outFile: for line in inFile: for key in replace: line = line.replace(key, replace[key]) outFile.write(line) spaces = len(line) line = line.lstrip() spaces -= len(line) if line.startswith( '<uses-feature') and not featuresWritten: print('\nUpdating features\n') for feature in features: print(' ' + feature) outFile.write(spaces * ' ' + '<uses-feature android:name="{}"/>\n' .format(feature)) featuresWritten = True if line.startswith( '<uses-permission') and not permissionsWritten: print('\nUpdating permissions\n') for permission in permissions: print(' ' + permission) outFile.write( spaces * ' ' + '<uses-permission android:name="{}"/>\n'. format(permission)) permissionsWritten = True os.remove(manifest) shutil.move(manifestTemp, manifest)
fname = 'Library.xml' # <key>Track ID</key><integer>369</integer> # <key>Name</key><string>Another One Bites The Dust</string> # <key>Artist</key><string>Queen</string> def lookup(d, key): found = False for child in d: if found: return child.text if child.tag == 'key' and child.text == key: found = True return None stuff = ET.parse(fname) all = stuff.findall('dict/dict/dict') print('Dict count:', len(all)) for entry in all: if lookup(entry, 'Track ID') is None: continue name = lookup(entry, 'Name') artist = lookup(entry, 'Artist') album = lookup(entry, 'Album') genre = lookup(entry, 'Genre') count = lookup(entry, 'Play Count') rating = lookup(entry, 'Rating') length = lookup(entry, 'Total Time') if name is None or artist is None or genre is None or album is None:
def pull_csv(wkly, datadir, txt_fields, ipc_fields): # 0320 failed, broken tag on line 6927131, the opening <li> a few lines above has an accidental </li> at the end of its line for w in wkly: print('Writing ' + w + '\n') with open(datadir + "/ipg18" + w + "/txt18" + w + ".csv", 'w', newline='') as txt_file: with open(datadir + "/ipg18" + w + "/ipc18" + w + ".csv", 'w', newline='') as ipc_file: write_txt = csv.DictWriter(txt_file, fieldnames=txt_fields) write_txt.writeheader() write_ipc = csv.DictWriter(ipc_file, fieldnames=ipc_fields) write_ipc.writeheader() e = ET.parse(datadir + "/ipg18" + w + "/ipg18" + w + ".fix").getroot() for child in e: if child.tag == 'us-patent-grant': pub = child.find('us-bibliographic-data-grant').find('publication-reference').find( 'document-id').find( 'doc-number').text ver = '' lev = '' sec = '' cla = '' subc = '' grp = '' subg = '' pos = '' cv = '' act = '' gen = '' sta = '' src = '' a = '' c = '' d = '' try: natctry = child.find('us-bibliographic-data-grant').find('classification-national').find( 'country').text natcla = child.find('us-bibliographic-data-grant').find('classification-national').find( 'main-classification').text except: a = [elem.text for elem in child.find('abstract').iter()] a = " ".join(x for x in a if x is not None) a = "".join(x for x in a if x in string.printable) c = [elem.text for elem in child.find('claims').iter()] c = " ".join(x for x in c if x is not None) c = "".join(x for x in c if x in string.printable) d = [elem.text for elem in child.find('description').iter()] d = " ".join(x for x in d if x is not None) d = "".join(x for x in d if x in string.printable) mute = write_txt.writerow({'pub': pub, 'typ': pub[0:2], 'desc': d.replace('\n', ' ').replace('\r', ' ').replace('"', ''), 'abstract': a.replace('\n', ' ').replace('\r', ' ').replace('"', ''), 'claims': c.replace('\n', ' ').replace('\r', ' ').replace('"', '') }) for c in child.find('us-bibliographic-data-grant').find('classifications-ipcr').findall( 'classification-ipcr'): ver = c.find('ipc-version-indicator').text lev = c.find('classification-level').text sec = c.find('section').text cla = c.find('class').text subc = c.find('subclass').text grp = c.find('main-group').text subg = c.find('subgroup').text pos = c.find('symbol-position').text cv = c.find('classification-value').text act = c.find('action-date').text gen = c.find('generating-office').text sta = c.find('classification-status').text src = c.find('classification-data-source').text mute = write_ipc.writerow({'pub': pub, 'tri': sec + cla + subc, 'ver': ver, 'lev': lev, 'sec': sec, 'cla': cla, 'subc': subc, 'grp': grp, 'subg': subg, 'pos': pos, 'cv': cv, 'act': act, 'gen': gen, 'sta': sta, 'src': src }) return
def parse_tile_map(tmx_file: Union[str, Path]) -> objects.TileMap: """Parse tile map. Args: tmx_file (Union[str, Path]): TMX file to be parsed. Returns: objects.TileMap: TileMap object generated from the TMX file provided. """ # setting up XML parsing map_tree = etree.parse(str(tmx_file)) map_element = map_tree.getroot() # positional arguments for TileMap parent_dir = Path(tmx_file).parent version = map_element.attrib["version"] tiled_version = map_element.attrib["tiledversion"] orientation = map_element.attrib["orientation"] render_order = map_element.attrib["renderorder"] map_width = int(map_element.attrib["width"]) map_height = int(map_element.attrib["height"]) map_size = objects.Size(map_width, map_height) tile_width = int(map_element.attrib["tilewidth"]) tile_height = int(map_element.attrib["tileheight"]) tile_size = objects.Size(tile_width, tile_height) infinite_attribute = map_element.attrib["infinite"] infinite = bool(infinite_attribute == "true") if "nextlayerid" in map_element.attrib: next_layer_id = int(map_element.attrib["nextlayerid"]) else: next_layer_id = None if "nextobjectid" in map_element.attrib: next_object_id = int(map_element.attrib["nextobjectid"]) else: next_object_id = None tile_sets = _get_tile_sets(map_element, parent_dir) layers = _get_layers(map_element) tile_map = objects.TileMap( parent_dir, tmx_file, version, tiled_version, orientation, render_order, map_size, tile_size, infinite, next_layer_id, next_object_id, tile_sets, layers, ) try: tile_map.hex_side_length = int(map_element.attrib["hexsidelength"]) except KeyError: pass try: tile_map.stagger_axis = map_element.attrib["staggeraxis"] except KeyError: pass try: tile_map.stagger_index = map_element.attrib["staggerindex"] except KeyError: pass try: color = parse_color(map_element.attrib["backgroundcolor"]) tile_map.background_color = (color.red, color.green, color.blue) except KeyError: pass properties_element = map_tree.find("./properties") if properties_element is not None: tile_map.properties = _parse_properties_element(properties_element) return tile_map
def parse_request(self, url): with urlopen(url) as response: tree = etree.parse(response) root = tree.getroot() return root
def load_data(choice): print("Loading {} dataset".format(choice)) data = [] if choice == "train": with open("./Winograd/train/train.c.txt") as f: content = f.readlines() i = 0 while i < len(content): q = schema(content[i], content[i+1], content[i+2].split(",")[0], content[i+2].split(",")[1], content[i+3]) data.append(q.to_dict()) i += 5 elif choice == "test1": with open("./Winograd/test1/test1.c.txt") as f: content = f.readlines() i = 0 while i < len(content): q = schema(content[i], content[i+1], content[i+2].split(",")[0], content[i+2].split(",")[1], content[i+3]) data.append(q.to_dict()) i += 5 elif choice == "test2": tree = etree.parse('./Winograd/test2/WSCollection.xml') root = tree.getroot() data = list() original_problems = root.getchildren() for original_problem in original_problems: problem = dict() for information in original_problem.getchildren(): if information.tag == 'answers': answers = information.getchildren() answer_list = list() for answer in answers: answer_list.append(answer.text.strip()) problem['answers'] = answer_list elif information.tag == 'text': texts = information.getchildren() text_dict = dict() for text1 in texts: text_dict[text1.tag] = text1.text.replace('\n', ' ').strip() problem['text'] = text_dict elif information.tag == 'quote': pass else: problem[information.tag] = information.text.replace(' ', '') data.append(problem) else: tree = etree.parse('./Winograd/test2/WSCollection.xml') root = tree.getroot() data = list() original_problems = root.getchildren() for original_problem in original_problems: problem = dict() for information in original_problem.getchildren(): if information.tag == 'answers': answers = information.getchildren() answer_list = list() for answer in answers: answer_list.append(answer.text.strip()) problem['answers'] = answer_list elif information.tag == 'text': texts = information.getchildren() text_dict = dict() for text1 in texts: text_dict[text1.tag] = text1.text.replace('\n', ' ').strip() problem['text'] = text_dict elif information.tag == 'quote': pass else: problem[information.tag] = information.text.replace(' ', '') data.append(problem) with open("./Winograd/test1/test1.c.txt") as f: content = f.readlines() i = 0 while i < len(content): q = schema(content[i], content[i+1], content[i+2].split(",")[0], content[i+2].split(",")[1], content[i+3]) data.append(q.to_dict()) i += 5 with open("./Winograd/train/train.c.txt") as f: content = f.readlines() i = 0 while i < len(content): q = schema(content[i], content[i+1], content[i+2].split(",")[0], content[i+2].split(",")[1], content[i+3]) data.append(q.to_dict()) i += 5 return data