looks like python treats regular strings as unicode

Advertisements

Looks like python3 treats regular strings as unicode…

import hashlib
h= hashlib.md5()
h.update ('abcd')

cause the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Unicode-objects must be encoded before hashing

forcing me to do encode it before:

import hashlib
h= hashlib.md5()
h.update ('abcd'.encode ('ascii', 'replace'))

which is tedious since the structure occurs several dozens of time in the program.

I was wondering if there is an alternative to not use encode everywhere in the program.

>Solution :

You can define an inline ASCII byte literal using the b prefix. i.e.

import hashlib

h = hashlib.md5()
h.update(b"abcd")

References

Leave a ReplyCancel reply