Skip to content

Instantly share code, notes, and snippets.

@initialed85
Last active November 28, 2018 10:29
Show Gist options
  • Save initialed85/a17885256caf5cc5c111fa5962d9ceae to your computer and use it in GitHub Desktop.
Save initialed85/a17885256caf5cc5c111fa5962d9ceae to your computer and use it in GitHub Desktop.
Acea's voltage percentage thing
import re
data = """
Charge voltage 3.3V 3.5V 3.6V 3.7V 3.8V 3.9V 4.0V 4.05V 4.1V 4.15V 4.2V 4.25V * 4.3V*
Percentage of 4.2V 0 % 2.9 % 5.0 % 8.6 % 36 % 62 % 73 % 83 % 89 % 94 % 100 % 105 % 106%
"""
# grab only the first real line
interesting = data.strip().split('\n')[0]
print(interesting)
# pluck the voltages out using regex
voltages = [
float(x.rstrip('V')) for x in re.findall(r'[0-9]\.[0-9]V', interesting)
]
print(voltages)
# turn them into percentages
percentages = [
round((x / 4.2) * 100, 2) for x in voltages
]
print(percentages)
# if we want to iterate them together
zipped = list(zip(voltages, percentages))
print(zipped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment