[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I need help to put the output from terminal in a csv file

On 07/09/18 13:55, catanaangelo at gmail.com wrote:
> HI! I need help putting the output from terminal in a csv file. I work on linux and I have python 2.7.15. I am running a python program which also runs some shell scripts.
> I need to capture the output from the .sh scripts and put it nicely in a csv table. I am using "commands.getoutput('bash example.sh')" to capture the output of the scripts (I can use subprocess too if needed).
> The outputs looks something like this:
> In case of an error
> {
>     "error" : {
>        "body" : "The requested device is currently not online.
>        "detail" : "Make sure to connect the device",
>        "event" : 123456,
>        "header" : "Device Unavailable (123456)"
>     }
> }
> And in case of a good output it look similar but has "status :0" instead of event and ofc it does say that it passed the test.
> I need to export the results of the .sh tests in a csv file with my .py program.
> The boss expects a table with 3 columns. The first one with The title TEST which has the name of the tests (.sh scripts). The 2nd with PASS/FAIL (this is determined by the "status" : 0 output for some tests and with a number between 1 and 4000 for others (i used regex for this, but I struggle a bit here too) and the 3rd one which returns the Value ( 0 in case status is 0 or the number between 1 and 4000 for the other tests).
> I really need some ideas, I struggle for 3 days already using regex, commands, subprocess and watching tutorials.
> I just can't solve this yet, I really need some ideas.

 From what you've said above, your basic problem is that you need to 
parse the output looking for lines containing '"status": <n>' where <n> 
is some number.

Let's assume that you have run one of the shell scripts and captured its 
output into a string.  The first thing to do is to split the output into 
lines, which you can do with the "split()" string method.  Then check 
each line in turn.  If it contains a colon it may be what you want. 
"split()" the line at the colon and look at the first half.  Once you 
have "strip()"ed off the spaces at the beginning and the end, is it 
"status"?  If so, you've got your pass and the VALUE you want is in the 
second half.

I hope that helps.

Rhodri James *-* Kynesim Ltd