Click here to Skip to main content
15,902,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a .c file and it contains several arrays. I need to parse a specific array which has the following name: s_InfoDay day_stat[] = { ... };

My python script has to be able to read this .c file and look for this specific array and perform the process on it. The c array has information in the following format:

s_InfoDay day_stat[] = {
//-------------------------//
//|  Rank        |   Stat   |    Level |       Stance | //
{ 0              , "ALPHA",  "NOTPRESENT", "NOTPRESENT" },
{  NA             , "LAURA",  "UNKNOWN",    "UNKNOWN" },
{  1 | LEAGUE_1   , "MANU" ,   "SECOND",    "ONGOING" },
{  2 | LEAGUE_2   , "MANU" ,   "SECOND",    "ONGOING" },
{  1 | TEAM_3 | (1<<3), "CHELSEA", " FIRST", "WINNERS" },
{  3 |(1<<3) | TEAM_3 , "CHELSEA", " FIRST", "WINNERS" },   
{  0                  , NULL     , NULL    , NULL      }  } ;
The array contains almost 500 lines and it has data in this specific format. I only want to be able to extract the values under the  |   Stat   |    Level | and able to place the in the form of an xml like structure for display purposes.

The xml tags has the following fixed format. I will have a group of 500 of these tags which have the correct values in the stat and level section in the xml tag below:


What I have tried:

Whenever I parse each line the array, I need to be able to place the string in " " under the Stat in the location of the STAT in the PrimierLeague/Information/STAT and same for the Level in the tag <Description>**Level**</Description>.


The xml format is shown below:
<League name="PrimierLeague/Information/**STAT**" TeamType="Team"    visibility="false">
    <Description>**Level**</Description>
    <Target><Property/></Target>
    <Info type="String">EMPTY</Info>
  </League >
Posted
Updated 9-Mar-17 7:19am
v2
Comments
Member 13048699 9-Mar-17 9:30am    
The xml has the follwinf format. I could not past all of it above there.
<League name="PrimierLeague/Information/**STAT**" TeamType="Team" visibility="false">
<description>**Level**
<target><property>
<Info type="String">EMPTY
</League >
CPallini 9-Mar-17 17:02pm    
An alternative could be making the C program producing the XML output.

1 solution

A possible implementation:

  • Declare and clear a variable indicating that the array is processed
  • Read file line by line:
    Python
    with open(path_to_file) as f:
        for line in f:
            process(line)
  • If array is processed
  • Else check for the array definition and set indicator if found

To create the XML strings use the String Formatting Operations[^].
 
Share this answer
 
v3
Comments
Member 13048699 9-Mar-17 9:45am    
I want to be able to use python and not php here. Can you help me with a solution regarding python? I would be grateful to you.
Jochen Arndt 9-Mar-17 9:48am    
Uups. I will change it. But should be the same principle.
Member 13048699 9-Mar-17 10:25am    
I am not sure on what you mean by this: Declare and clear a variable indicating that the array is processed. Could you provide me with an example? Thanks in advance.
Member 13048699 9-Mar-17 10:27am    
Could you please provide me with some code layout as I am relatively new to python. I have experience mostly in C++. I would be grateful to you. I am sorry if I am troubling you but I am in real need of help.
Member 13048699 9-Mar-17 10:42am    
@Jochen Arndt Currently i am able to print the opened file in python code. Unsure on how to implement the process() function. May you please help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900