skip to Main Content

I am using python3 to receive and process text messages from a telegram channel. I sometimes get messages containing a string like this:

Ехchanges: Віnance Futures

Looking pretty normal. But when I want to check

if 'Exchanges' in the_string:

I get

False

Trying to track this down:

the_string.encode()

yields

b'xd0x95xd1x85changes: xd0x92xd1x96nance Futures'

How can I convert this to a usual string?

'Exchanges: Binance Futures'

2

Answers


  1. Try to use encode() and decode() methods of the str class mixed together:

    >>> my_string = 'Ехchanges: Віnance Futures'
    >>> 'Ехchanges' in my_string
    True
    >>> my_string.encode()
    b'xd0x95xd1x85changes: xd0x92xd1x96nance Futures'
    >>> 'Ехchanges' in my_string.encode().decode()
    True
    >>> 
    
    Login or Signup to reply.
  2. It’s utf-8 encoded string. You need to use string decoder decode('utf-8') here.

    Solution:

    encoded_string = b'xd0x95xd1x85changes: xd0x92xd1x96nance Futures'
    decoded_string = encoded_string.decode("utf-8")
    print(decoded_string)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search