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
-
- Posts: 59
- Joined: Wed Jun 06, 2018 12:54 am
Re: Command Line Output to Excel Document
I am sure Excel can read CSV format files (comma separated variables) which are trivial to write.
- Z80 Refugee
- Posts: 359
- Joined: Sun Feb 09, 2014 1:53 pm
Re: Command Line Output to Excel Document
Creating a native Excel file will be a complex process - don't bother banging your head on that one.bmattila55 wrote: ↑Wed Jun 06, 2018 6:42 amAny ideas on how to get this to output to an Excel document (or even just a normal file but preferably straight to Excel)
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.
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.bmattila55 wrote: ↑Wed Jun 06, 2018 6:42 amI 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.
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
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)!
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)!
-
- Posts: 59
- Joined: Wed Jun 06, 2018 12:54 am
Re: Command Line Output to Excel Document
Thanks, XlsxWriter looks good.B.Goode wrote: ↑Wed Jun 06, 2018 6:54 amMaybe https://xlsxwriter.readthedocs.io, or simply https://docs.python.org/3/library/csv.html
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
Re: Command Line Output to Excel Document
XlsxWriter is a Python module for creating Excel XLSX files. Surely you would want to incorporate that into your own script?Any ideas on how to incorporate it into the XlsxWriter
After initialisation of a workbook and worksheet, maybe you would simply replace the existing
Code: Select all
print(measure_temp())
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.
-
- Posts: 59
- Joined: Wed Jun 06, 2018 12:54 am
Re: Command Line Output to Excel Document
Thanks I'll look into that. There will be approximately 2500 entries going in so hope it can handle all that.
- Z80 Refugee
- Posts: 359
- Joined: Sun Feb 09, 2014 1:53 pm
Re: Command Line Output to Excel Document
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.
2,500 lines is a mere trifle. No problem at all.bmattila55 wrote: ↑Wed Jun 06, 2018 9:06 amThere will be approximately 2500 entries going in so hope it can handle all that.
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)!
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)!
Re: Command Line Output to Excel Document
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...
-
- Posts: 59
- Joined: Wed Jun 06, 2018 12:54 am
Re: Command Line Output to Excel Document
Ok so it would be something like pi@whatever $ sudo monitor-temp.py > data.csv ???Z80 Refugee wrote: ↑Wed Jun 06, 2018 9:11 amAssuming 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.
Re: Command Line Output to Excel Document
Something like that. Not sure why it needs root/superuser privilege?so it would be something like pi@whatever $ sudo monitor-temp.py > data.csv ???
-
- Posts: 59
- Joined: Wed Jun 06, 2018 12:54 am
Re: Command Line Output to Excel Document
Probably true. Thanks for everyone's help
- Z80 Refugee
- Posts: 359
- Joined: Sun Feb 09, 2014 1:53 pm
Re: Command Line Output to Excel Document
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)!
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)!