Home > Net >  Saving API terminal output to JSON file
Saving API terminal output to JSON file

Time:09-15

I am exporting data from a sensor. The command to download the logged data from the sensor is:

e = Event()
libmetawear.mbl_mw_logging_download(d.board, 0, byref(download_handler))
e.wait()

When ran, this outputs the data in the terminal in a format as follows:

{epoch: 1663184089978, value: {x : 0.177, y : -0.060, z : 0.993}}
{epoch: 1663184089988, value: {x : 0.179, y : -0.059, z : 0.993}}
{epoch: 1663184089998, value: {x : 0.179, y : -0.060, z : 0.995}}
{epoch: 1663184090009, value: {x : 0.180, y : -0.060, z : 0.994}}

I would like to save this to a file. Given the output format, I'm attempting to save to a JSON file (eventually, I'd like to save it to CSV). However, assigning a variable returns 'None' with format NoneType and trying to write a file (see below) returns 'null' within the file:

with open('data.json', 'w') as fp:
    json.dump(libmetawear.mbl_mw_logging_download(d.board, 0, byref(download_handler)), fp)
    

This may have to do with the 'e.wait()' command not being called or a formatting issue. Any help is appreciated!

CodePudding user response:

I didn't work with that package before, so if there is not an option to retrieve the string representation of the JSON response and it just prints it to the terminal, use the following:

Change the sys.stdout. It is where the output goes and by default sys.stdout is connected to the terminal:

import sys

def print_to_terminal():
    """Simulate the output"""
    print("{epoch: 1663184089978, value: {x : 0.177, y : -0.060, z : 0.993}}")
    print("{epoch: 1663184089988, value: {x : 0.179, y : -0.059, z : 0.993}}")
    print("{epoch: 1663184089998, value: {x : 0.179, y : -0.060, z : 0.995}}")
    print("{epoch: 1663184090009, value: {x : 0.180, y : -0.060, z : 0.994}}")


with open("output.txt", "w", encoding="utf-8") as f:
    sys.stdout = f

    # calling your function
    print_to_terminal()

    # restoring it back to default
    sys.stdout = sys.__stdout__

Another option is of course just running your python file and redirect the stdout using shell feature(here BASH):

python your_script.py > output.txt

This sets the standard output to the output.txt so it's no longer the terminal and you don't need to change (add) anything to your code.

If you also want to redirect the standard error(where the error messages go), you know what to change --> sys.stderr. You can set them to the same file.

and for second option use:

python your_script.py > output.txt 2>&1

CodePudding user response:

It's hard to answer correctly as I can't run your code.

I guess that the printing is done by the mbl_mw_logging_download function. You can redirect the stdout into a file (official Python doc here).

In your case, it could be done like this:

with open('data.json', 'w') as fp:
    with redirect_stdout(fp):
        json.dump(libmetawear.mbl_mw_logging_download(d.board, 0, byref(download_handler)), fp)

The written file is not a json but you can read it after for working on its content.

  • Related