"LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs In Python

Dung Do Tien Jun 06 2021 5

I'm a newbie in Python and now I want to encode a text string to base64. I have some line code below:

 phone = "0234566778"
name ="Jonh"
print('%s is trying encode his phone in base64 = %s\n'%(name,phone.encode('base64','strict')))

When running this code I got an error:

 LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs 

LookupError: base64 is not a text encoding in python

How can I resolve it?

Have 2 answer(s) found.
  • M

    Mai Nguyen Nhat Dang Jun 06 2021

    You need to import base64 package. It helps convert encode text string to base 64. See example:

     import base64
    base64.b64encode(bytes('your tex string here', 'utf-8'))

    I hope it works for you.

  • j

    jrobertojrmx Jose Roberto Jun 06 2021

    You can try a simple way below:

     import base64
    base64.b64encode('Your string text here'.encode('ascii'))  

    And it works for me.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close