0

I want to use the command:

subprocess.check_output(["powercfg", "-list"])

which gives me:

Existing Power Schemes (* Active)

-----------------------------------

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Balanced)

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (High performance)

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Power saver) *

which is good. But, I only want the variables in the middle to go into their own variables without all the extra text. The problem is that the command comes out in multiple lines so I can't use (list[19:55]). I also need to do this with each of the strings inside the parentheses into their own variables but I think I can figure that out. I'm new to Python so I'm open to whatever.

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
jangles
  • 303
  • 1
  • 6
  • 22
  • 1
    well, split the lines that check_output returns and process them one by one. – Jean-François Fabre Aug 05 '18 at 20:06
  • @Jean-FrançoisFabre Ok so I tried `list = subprocess.check_output(["powercfg", "-list"]).splitlines()` and it gave me all of it on one line and I don't know how I would extract the strings I need from that. – jangles Aug 05 '18 at 20:18
  • Which parts do you want to extract? The xxx's or the text in the parenthesis? – aaldilai Aug 05 '18 at 20:26
  • @aaldilai Ideally I want both but I need the xxx's first. – jangles Aug 05 '18 at 20:30
  • Okay so now I have `print(list.splitlines())` and each line is its own string. So how do I now use them each individually? – jangles Aug 05 '18 at 20:43

3 Answers3

0

Found the answer, even though my title is poorly worded I hope someone finds this because it was hell trying to figure it out. All you have to do is change the number at the end to go down lines:

firstplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[3]

secondplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[4]

etc.

I then went on to "parse" the ID strings out (if that's the right word) with this:

firstplanID = ((firstplan.split(": "))[1].split(" (")[0])

and then manually set a variable for each plan.

jangles
  • 303
  • 1
  • 6
  • 22
0

you may try this:

data = '''Existing Power Schemes (* Active)

-----------------------------------

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Balanced)

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (High performance)

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Power saver) *'''

values = [i.split()[3] for i in data.splitlines() if i.startswith('Power')]

the results:

>>> values
[
    'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
]
lenik
  • 23,228
  • 4
  • 34
  • 43
0

You can try using regex.

import re

pattern = "Power Scheme GUID: ([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})\s+\(([a-zA-Z ]+)\)"
mystr = "Existing Power Schemes (* Active)\n-----------------------------------\nPower Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Balanced)\nPower Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (High performance)\nPower Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Power saver) *"

re.findall(pattern, mystr)
# results: [('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Balanced'), ('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'High performance'), ('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Power saver')]

Given a string and a pattern, you can find all the parts of the string that match your pattern. In this case, I created a pattern, and grouped (using paranthesis) the id part and the type part. The result is a list of tuples. Each tuple is a pattern match.

aaldilai
  • 271
  • 1
  • 6