Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

looks like python treats regular strings as unicode

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading