bmattila55
Posts: 59
Joined: Wed Jun 06, 2018 12:54 am

Command Line Output to Excel Document

Wed Jun 06, 2018 6:42 am

Hi,

I was wondering if it is possible to get the CLI to output data to an excel file.
I have a python script that measures the temperature of the CPU every x seconds.
I want to have this running over about 7 hours.
Any ideas on how to get this to output to an Excel document (or even just a normal file but preferably straight to Excel)

Thanks so much, Ben

User avatar
jahboater
Posts: 6663
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 6:52 am

I am sure Excel can read CSV format files (comma separated variables) which are trivial to write.


User avatar
Z80 Refugee
Posts: 359
Joined: Sun Feb 09, 2014 1:53 pm

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 7:03 am

bmattila55 wrote:
Wed Jun 06, 2018 6:42 am
Any ideas on how to get this to output to an Excel document (or even just a normal file but preferably straight to Excel)
Creating a native Excel file will be a complex process - don't bother banging your head on that one.

The good news is that virtually all spreadsheet programs can take a text format called CSV (comma separated values). Each line in the CSV file represents a row on the spreadsheet, and commas in the CSV file separate the data for each cell (columns). There will be some intelligent formatting going on: if a CSV value "looks like" a number it will be read into the spreadsheet as a number, otherwise it will be taken as a text string (but you can't have commas in your text strings).

As an alternative to commas, usually you can use Tab characters instead.

In your case, if all you have in your output is a series of temperature values, all you need is one value per line (no need for separator characters at all).

You can then open the CSV file directly in Excel (or whatever), or open a new spreadsheet and import the CSV file.
bmattila55 wrote:
Wed Jun 06, 2018 6:42 am
I was wondering if it is possible to get the CLI to output data to an excel file.
I have a python script that measures the temperature of the CPU every x seconds.
I'm not a Python programmer, but surely it's possible to write directly to a file in Python code??? That would be the right way to do it.

To do it from the Linux command prompt, all you need to do is divert the standard output stream to a file. If you were to type the following:

Code: Select all

mycode > myoutput.csv
...the executable called "mycode" would then send the output you normally see on-screen to a file called "myoutput.csv". There are ways to send output to screen as well as a file - google "Linux tee".
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

bmattila55
Posts: 59
Joined: Wed Jun 06, 2018 12:54 am

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 7:34 am

Thanks, XlsxWriter looks good.

import os
import time

def measure_temp():
temp = os.popen("vcgencmd measure_temp").readline()
return (temp.replace("temp=",""))

while True:
print(measure_temp())
time.sleep(2)

This is the code I have. Any ideas on how to incorporate it into the XlsxWriter

User avatar
B.Goode
Posts: 11168
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 8:33 am

Any ideas on how to incorporate it into the XlsxWriter
XlsxWriter is a Python module for creating Excel XLSX files. Surely you would want to incorporate that into your own script?

After initialisation of a workbook and worksheet, maybe you would simply replace the existing

Code: Select all

print(measure_temp())  
with a call to worksheet.write()?

If that is too complex for your needs, just do the simple thing and use print statements to create a CSV file and then redirect the output from the script to a file, as suggested in another post here.

bmattila55
Posts: 59
Joined: Wed Jun 06, 2018 12:54 am

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 9:06 am

B.Goode wrote:
Wed Jun 06, 2018 8:33 am
Any ideas on how to incorporate it into the XlsxWriter
If that is too complex for your needs, just do the simple thing and use print statements to create a CSV file and then redirect the output from the script to a file, as suggested in another post here.
Thanks I'll look into that. There will be approximately 2500 entries going in so hope it can handle all that.

User avatar
Z80 Refugee
Posts: 359
Joined: Sun Feb 09, 2014 1:53 pm

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 9:11 am

B.Goode wrote:
Wed Jun 06, 2018 8:33 am
If that is too complex for your needs, just do the simple thing and use print statements to create a CSV file and then redirect the output from the script to a file, as suggested in another post here.
That's my recommendation. Outputting to as plain a file as possible, and then using the import capability of a spreadsheet to create your graphs or whatever as a back-end process, is much more flexible than committing to complexity at the front end. You can then take the same data and use it in a completely different process in the future, without having to extract it from a dedicated spreadsheet file.

Assuming you have no user input as part of the Python code (so it runs from the command line stand-alone), diverting the output to a file is a perfectly acceptable way to go with no need to code explicit file writes into the Python. In fact, I take back what I said earlier about doing it in Python. Using the command line to send the output to a file instead of (or as well as) the screen means you can do what you like with it without any rewrites to the Python - even pipe it into another process.
bmattila55 wrote:
Wed Jun 06, 2018 9:06 am
There will be approximately 2500 entries going in so hope it can handle all that.
2,500 lines is a mere trifle. No problem at all.
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

User avatar
B.Goode
Posts: 11168
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 9:19 am

There will be approximately 2500 entries going in so hope it can handle all that.


If it concerns you, do a quick estimate...

Each reading will probably be represented by no more than 10 digits or characters?

So a set of readings will contain 2500 lines and a maximum of 25000 bytes.

There are very many files installed with the Raspbian Operating System that have sizes greater than either of those metrics. I don't think you need to worry...

bmattila55
Posts: 59
Joined: Wed Jun 06, 2018 12:54 am

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 10:24 am

Z80 Refugee wrote:
Wed Jun 06, 2018 9:11 am
Assuming you have no user input as part of the Python code (so it runs from the command line stand-alone), diverting the output to a file is a perfectly acceptable way to go with no need to code explicit file writes into the Python. In fact, I take back what I said earlier about doing it in Python. Using the command line to send the output to a file instead of (or as well as) the screen means you can do what you like with it without any rewrites to the Python - even pipe it into another process.
Ok so it would be something like pi@whatever $ sudo monitor-temp.py > data.csv ???

User avatar
B.Goode
Posts: 11168
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 10:31 am

so it would be something like pi@whatever $ sudo monitor-temp.py > data.csv ???
Something like that. Not sure why it needs root/superuser privilege?

bmattila55
Posts: 59
Joined: Wed Jun 06, 2018 12:54 am

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 10:58 am

Probably true. Thanks for everyone's help

User avatar
Z80 Refugee
Posts: 359
Joined: Sun Feb 09, 2014 1:53 pm

Re: Command Line Output to Excel Document

Wed Jun 06, 2018 12:24 pm

Simples.
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

Return to “Beginners”