Open wujj123456 opened 4 years ago
Where were you when this was discussed? https://askubuntu.com/questions/1195766/problem-parsing-piped-command-streams-in-python-need-help Thanks!, python, Linux, et.al. is new to me. I retire when my company switched to Linux from Windows. The current problem I have in parsing is the decimal value after the dot cannot easily be obtained using my procedure. Could not easily get rid of the "degree centigrade" symbol when parsing.
going to try to get the missing decimal using differnt parsing
Oops, didn't realized I turned off github comment notifications. Sorry for the super late reply. I commented mostly because I happen to be looking into parsing these and thus learnt these shortcuts. I should have given some snippets, using python 3.8+.
For sensors, this gives you a dict to iterate through
import json
import subprocess
output = json.loads(
subprocess.run(
["sensors", "-j"], capture_output=True
).stdout.decode("utf-8")
)
# output
import pprint
pprint.pprint(output)
For nvidia-smi:
import subprocess
output = subprocess.run(
["nvidia-smi", "stats", "-d", "temp", "-c", "1"], capture_output=True
).stdout.decode("utf-8")
# output, "nvidia-smi stats --help" explains which field means
for line in output.split("\n"):
print(line)
I later realized though nvidia-smi doesn't support json output, it has -x flag to generate xml, which could help too.
Honestly, it's probably not worth touching it until next time you need to improve it or change it. I don't have all the different hardware to test the changes and thus I am just commenting here instead of a pull request.
https://github.com/JStateson/BoincTasks/blob/0ad25e7dc51bfb41def1706c58f95d0c98dce71a/SystemdService/FetchTemps.py#L92
"nvidia-smi stats -d temp -c 1". This would give you a CSV formatted output. It would probably make your parsing much easier.
Similarly, "sensors -j" gives a json you can directly do json.loads()