-2

I'd like to return the value of left battery capacity parsed from the given string. It means I want to get CurrentCapacity / MaxCapacity.

data = '''
    "SuperMaxCapacity" =0
    "MaxCapacity": +4540;
    'CurrentCapacity'=   2897,
    "LegacyBatteryInfo" = {"Amperage"=18446744073709550521,"Flags"=4,"Capacity"=4540,"Current"=2897,"Voltage"=7283,"Cycle Count"=406}
    "MegaMaxCapacity" = 6700
'''
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

0

This will do the job quite nicely, and will get the match, even though your data input format is quite iffy:

import re

data = '''
    "SuperMaxCapacity" =0
    "MaxCapacity": +4540;
    'CurrentCapacity'=   2897,
    "LegacyBatteryInfo" = {"Amperage"=18446744073709550521,"Flags"=4,"Capacity"=4540,"Current"=2897,"Voltage"=7283,"Cycle Count"=406}
    "MegaMaxCapacity" = 6700
'''

max_capacity = re.search(r"[\"']MaxCapacity.*?[:=].*?(\d+)", data).group(1)
current_capacity = re.search(r"[\"']CurrentCapacity.*?[:=].*?(\d+)", data).group(1)
print("Max capacity:", max_capacity)
print("Current capacity:", current_capacity)

Output:

Max capacity: 4540
Current capacity: 2897
ruohola
  • 21,987
  • 6
  • 62
  • 97