Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using pyton to code a small script which fills up an xml template which I define. The python code read an C file which has an array of strings seperated my commas. Something like this shown below.

    array myvector[] = {
    
    {"1" , "March", "1st", "2017"},
    {"2" , "March", "2nd", "2017"},
    {"3" , "March", "3rd", "2017"},
    .....
    {"1" , "April", "1st", "2017"},
    ....
    {"31" , "December", "31st", "2017"}
     {NULL , NULL, NULL, NULL}
    };


My python script replaces the strings like "March", "April", and ... till "December" in the {month} and number 1st, 2nd, 3rd, ... in the {day} part in the xml structure.

The funny thing is when I run my script, everything looks good except the last two days of December. The xml structure is only partly printed out. When I add an extra space or remove and extra sapce from **xmlTemplate** the printout of the xml structure for the last entries like the days in December get partly printed. I have no idea what is going wrong. Does it have to do with the formating of the xmlTemplate. Could someone advice me on what to do?

What I have tried:

I tried formating the spaces in the template but it does not help.
Posted
Updated 10-Mar-17 4:59am
v8
Comments
Jochen Arndt 10-Mar-17 9:52am    
You should show the code that generates the output file.

One reason for such behaviour is reading a file that is still opened (has not yet been closed).
Member 13051048 10-Mar-17 10:11am    
To have a clear understand of what I am telling, I have posted the output I get the output file below as an answer so it allows better formating. Could you please advice me on how to solve it?
Jochen Arndt 10-Mar-17 10:17am    
1. Don't post such as a solution
Use the green "Improve question" link to edit your question and delete the solution (it will probably reported as "not an answer").

2. It does not help much to solve the problem when seeing what is printed while not knowing how it is generated.
Member 13051048 10-Mar-17 10:26am    
Hi, I just posted my full code above in the edited question. I think this will give a better idea now? I would be grateful to you if you could help me resolve this bug. Thanks alot!
Jochen Arndt 10-Mar-17 10:35am    
Where is mytestfile opend and closed?

When and where do you read the content of the file?

When you are not closing the file with mytestfile.close() and reading it from the same python script or while the script has not executed you will get that behaviour as already noted.

1 solution

See the comments.

The problem source was not closing the output file before reading it later:
Python
for line in f:
    # ...
    mytestfile.write((xmlTemplate.format(month=monthone, day=dayone)))
    # ...
# Close files here
f.close()
mytestfile.close()
 
Share this answer
 

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