0

the regular expression is not working. please find the below details.

cpu_pattern = re.compile('.*CPU.*(usr|user).*nice.*sys.*')

part = b'11:40:24 AM     CPU      %usr     %nice      %sys   %iowait    %steal      %irq     %soft    %guest     %idle\n11:40:25 AM     all      0.00      0.00      0.08      0.00      0.00      0.00      0.00      0.00     99.92'

IF condition:

if cpu_pattern.search(part): if cpu_usage == '': cpu_usage == part

Error:

TypeError('cannot use a string pattern on a bytes-like object')
Alexander Golys
  • 683
  • 8
  • 23

2 Answers2

1

Please use below code to convert byte to string in python which will solve your issue:

part= part.decode("utf-8") 

Put above code after part obejct

Output print(part):

11:40:24 AM     CPU      %usr     %nice      %sys   %iowait    %steal      %irq     %soft    %guest     %idle                         
11:40:25 AM     all      0.00      0.00      0.08      0.00      0.00      0.00      0.00      0.00     99.92      
Ashish Karn
  • 1,127
  • 1
  • 9
  • 20
0

part is not a string, it’s a byte sequence. You now have two choices; which is more appropriate depends on how part was generated:

  1. Perform byte-wise matching rather than string matching, by using a byte sequence pattern:

    cpu_pattern = re.compile(b'.*CPU.*(usr|user).*nice.*sys.*')
    
  2. Decode the parts byte sequence using an appropriate encoding, e.g. UTF-8:

    parts_str = parts.decode('utf-8')
    
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214