python return list from linux command output
Question: I am new to python and I'm learning rapidly, but this is beyond my current
level of understanding. I'm trying to to pull the output from the linux
command apcaccess into a list in python.

apcaccess is a linux command to get the status of an APC UPS. The output is
this:

    
    
    $ apcaccess
    APC      : 001,035,0933
    DATE     : 2014-11-12 13:38:27 -0500  
    HOSTNAME : doormon
    VERSION  : 3.14.10 (13 September 2011) debian
    UPSNAME  : UPS
    CABLE    : USB Cable
    DRIVER   : USB UPS Driver
    UPSMODE  : Stand Alone
    STARTTIME: 2014-11-12 12:28:00 -0500  
    MODEL    : Back-UPS ES 550G 
    STATUS   : ONLINE 
    LINEV    : 118.0 Volts
    LOADPCT  :  15.0 Percent Load Capacity
    BCHARGE  : 100.0 Percent
    TIMELEFT :  46.0 Minutes
    MBATTCHG : 5 Percent
    MINTIMEL : 3 Minutes
    MAXTIME  : 0 Seconds
    SENSE    : Medium
    LOTRANS  : 092.0 Volts
    HITRANS  : 139.0 Volts
    ALARMDEL : 30 seconds
    BATTV    : 13.6 Volts
    LASTXFER : No transfers since turnon
    NUMXFERS : 2
    XONBATT  : 2014-11-12 12:33:35 -0500  
    TONBATT  : 0 seconds
    CUMONBATT: 53 seconds
    XOFFBATT : 2014-11-12 12:33:43 -0500  
    STATFLAG : 0x07000008 Status Flag
    SERIALNO : 4B1335P17084  
    BATTDATE : 2013-08-28
    NOMINV   : 120 Volts
    NOMBATTV :  12.0 Volts
    FIRMWARE : 904.W1 .D USB FW:W1
    END APC  : 2014-11-12 13:38:53 -0500
    

I've tried different iterations of `Popen` such as:

    
    
    def check_apc_ups():
      output = subprocess.Popen("apcaccess", stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
      x1, x2, x3, x4, x5 = output
    

I would like to be able to pull each line into a list or tuple containing all
32 and then only display/print what I need, such as `TIMELEFT` and `BCHARGE`.

Any help would be greatly appreciated.


Answer: 
    from subprocess import check_output
    out = check_output(["apcaccess"])
    
    spl = [ele.split(":",1)for ele in out.splitlines()]
    d = {k.rstrip():v.lstrip() for k,v in spl}
    
    print(d['BCHARGE'])
    print(d["TIMELEFT"])
    100.0 Percent
    46.0 Minutes
    
    
    from subprocess import check_output
    def get_apa():
        out = check_output(["apcaccess"])
        spl = [ele.split(":", 1) for ele in out.splitlines()]
        d = {k.rstrip(): v.lstrip() for k, v in spl}
        return d
    
    output = get_apa()
    print (output['BCHARGE'])
    100.0 Percent
    

To print all key/values pairings:

    
    
    for k,v in get_apa().items():
        print("{} = {}".format(k,v))
    

