1

I have a simple python module as follows:

from kafka import KafkaConsumer

consumer = KafkaConsumer(
    'myTestTopic',
    bootstrap_servers=['localhost:9092'],
    auto_offset_reset='earliest',
    enable_auto_commit=True)

for message in consumer:
    message = message.value
    print(message)

This will return something like follows:

b'&name=bob&gender=M&type=student&sId=1204&note=temp&'

b'&name=bob&gender=M&type=student&sId=1204&'

My question is: Is there a neat way to remove/trim the first two characters e.g: (b') and the last character (') from each line that will be returned?

So essentially it will appear like:

&name=bob&gender=M&type=student&sId=1204&note=temp&

&name=bob&gender=M&type=student&sId=1204&

Community
  • 1
  • 1
Saffik
  • 911
  • 4
  • 19
  • 45
  • Does this answer your question? [Convert bytes to a string](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – Aaron_ab Dec 16 '19 at 14:25

2 Answers2

2

That happens because the type of your response is bytes not string. You need to decode it, something like this should work:

message = str(message.value, encoding='utf-8')
print(message)
marcos
  • 4,473
  • 1
  • 10
  • 24
1

do:

message.value.decode('utf-8')
Aaron_ab
  • 3,450
  • 3
  • 28
  • 42