The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 232     Votes:  1     ✅ Solution
Tags: bash   python  
Link: 🔍 See Original Answer on Ask Ubuntu ⧉ 🔗

URL: https://askubuntu.com/q/1195775
Title: Problem parsing piped command streams in python, need help
ID: /2019/12/13/Problem-parsing-piped-command-streams-in-python_-need-help
Created: December 13, 2019
Upload: September 15, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


I’m in the same boat sailing from Bash Basin to Python Peninsula. I had to use a series of xrandr and wmctrl commands piped through grep for my inaugural project (still on-going sigh). Your solution would be:

$ sensors | grep Core
Core 0:        +47.0°C  (high = +100.0°C, crit = +100.0°C)
Core 1:        +49.0°C  (high = +100.0°C, crit = +100.0°C)
Core 2:        +45.0°C  (high = +100.0°C, crit = +100.0°C)
Core 3:        +47.0°C  (high = +100.0°C, crit = +100.0°C)
$ sensors.py
49
51
47
48

Here’s your script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
list_cores = os.popen("sensors | grep Core").read().splitlines()

for l in list_cores:

    # Core 0:        +47.0°C  (high = +100.0°C, crit = +100.0°C)  <---- sample
    #                ^  ^
    #                |  |
    #                |  +---- 2md split on decimal take [0] element (b list)
    #                +------- 1st aplit on plus sign take [1] element (a list)

    a = l.split("+") 
    b = a[1].split(".")
    c = b[0]
    print (c)

Mark as executable before running: chmod a+x sensors.py

Happy to answer any questions :)

⇧ Ubuntu 18.04 Boots Even Slower After Disabling Networking & Putting 'noresume' to the Boot kernel? How to set up night light mode on one monitor and the second one normal  ⇩