Нужна помощь с синтаксическим анализом XML с помощью Python
My Python code reads in a test.c file. In this file, there are many arrays with data. I am reading a specific array with the name string check_array[]. The array has the format shown below: string check_array[] = { { 1 , "NULL","NULL", "NULL", "Movies,Sports,Games" }, { 2 , "Day1", "Day1Task", "List", "WakeUp,Eat,Play,Sleep" }, { 3 , "Day3", "Day3Task", "List", "WakeUp,Eat,Play,Study" }, .... and so on BUT the array ends using like this.... { 0 , NULL, NULL, NULL, NULL, NULL} }; My Python code reads this array from the .c file as there are many other arrays as well. Then it builds an XML structure as shown below: xml= """ <Task name="TaskListForYear/DayList/{parta}"> <Info>{partb}</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task>""" In order to build the XML, I only focus on the second and third column strings. I do something like this in my code to filter those two columns: tmp1 = findstring.group(2).strip() tmp2 = findstring.group(3).strip() Example of XML structure written to file: <Task name="TaskListForYear/DayList/Day1Task"> <Info>List</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task> In other words, my Python code builds the XML structure for each line parsed in the array and writes this XML structure to a file using the following syntax: FileCheck.write((xml.format(parta=tmp1, partb=tmp2))). But now I want to make an improvement in my code. For example, if you look at the row: { 2 , "Day1", "Day1Task", "List", "WakeUp,Eat,Play,Sleep" }, Currently, I only build an XML structure like this for it: <Task name="TaskListForYear/DayList/Day1Task"> <Info>List</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task> I want to be able to do something like this now: <Task name="TaskListForYear/DayList/WakeUp_Day1Task"> <Info>List</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task> <Task name="TaskListForYear/DayList/Eat_Day1Task"> <Info>List</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task> <Task name="TaskListForYear/DayList/Play_Day1Task"> <Info>List</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task> <Task name="TaskListForYear/DayList/Study_Day1Task"> <Info>List</Info> <Task type="To be filled soon">Not_filled_as_of_now</Value> </Task> Note that the WakeUp, eat, play, and sleep come in front of the Day1Task. I am not sure how to do this. Can someone provide me with example code? My code is as follows: start = False with open(mygamesinfo.c) as g: for ln in g: if re.search("NULL", line): start=False if start: matchingstring= re.search(r"{(.*?),(.*?),(.*?),(.*?),.*?)}", line) if matchingstring: tag = matchingstring.group(2).strip() name = matchingstring.group(3).strip() if tmp1== "NULL": tmp1= None else: tmp1= tmp1 [1:-1] if tmp2== "NULL": tmp2= None else: tmp2= tmp2[1:-1] FileCheck.write((xmlTemplate.format(parta=tmp2,partb=tmp2))) elif (line.startswith('string check_array[] = {')): start =trueЯ
Что я уже пробовал:
Это показано выше. Я реализовал часть кода, которая выполняет часть задачи. Мне нужно сделать другую часть сейчас.