How to remove b character in python3

How can I remove a character b

from a python3 script?

import subprocess

get_data=subprocess.check_output(["df -k | awk '{print $6}'"],shell=True)
data_arr=get_data.split()
data_arr.pop(0)
data_arr.pop(0)
for i in data_arr[:]:
    print(str(i))

      

Output

b'/dev/shm'
b'/run'
b'/sys/fs/cgroup'
b'/'
b'/tmp'
b'/test'
b'/boot'
b'/home'
b'/var'
b'/mnt/install'
b'/mnt/snapshot'
b'/mnt/share'
b'/mnt/storage'
b'/mnt/linux'
b'/mnt/download'
b'/run/user/1001'

      

+3


source to share


1 answer


The symbol b

indicates that the output check_process

is equal bytes

, not str

. The best way to remove it is to convert the output to a string before doing any further work on it:

byte_data=subprocess.check_output(["df -k | awk '{print $6}'"],shell=True)
str_data = byte_data.decode('utf-8')
data_arr=str_data.split()
...

      

The method decode

will take care of any unicode you may have in the string. If your default encoding (or the one in use awk

I suppose) is not UTF-8

, replace it with the correct one in the example above.

Perhaps an even better way to work around this issue is to report check_output

open stdout

as a text stream. The easiest way is to add an argument universal_newlines=True

that will use the default encoding for your current locale:



str_data = subprocess.check_output(["df -k | awk '{print $6}'"], shell=True, universal_newlines=True)

      

Alternatively, you can specify an explicit encoding:

str_data = subprocess.check_output(["df -k | awk '{print $6}'"], shell=True, encoding='utf-8')

      

In both cases, you don't need to decode, because the result will be narrower str

, not bytes

.

+3


source







All Articles