I'm trying to take the output of upower -d
(shell command) and split it into a long list using .split() so it can be searched.
When I do
import subprocess
dump = subprocess.check_output(["upower", "-d"])
print(dump.split())
it will print the output in list form as expected except every element in the list is preceded with a "b" (not inside the string).
When I do the same in python 2.7 it gives me the output I expect but I would like it to be in python 3.
b""
is a bytes
literal in Python. In Python 2.7, ""
is also a bytestring. print(your_list)
prints representations (repr
) of each item that is why you see b""
in Python 3 but not in Python 2.
subprocess.check_output()
returns bytes in both Python 2 and 3 unless universal_newlines=True
specified in Python 3 that uses locale.getpreferredencoding(False)
to decode the bytes.
from subprocess import check_output
output = check_output(["upower", "-d"], universal_newlines=True)
print(output.split())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With