hashlib.md5() TypeError: Unicode-objects must be encoded before hashing

后端 未结 7 1222
自闭症患者
自闭症患者 2020-12-09 15:16

I am new to coding and have ran into a problem trying to encode a string.

>>> import hashlib
>>> a = hashlib.md5()
>>> a.update(\'         


        
7条回答
  •  攒了一身酷
    2020-12-09 15:29

    Since you are encoding simple strings I deduce that you are running Python 3 where all strings are unicode objects, you have two options:

    1. Provide an encoding for the strings, e.g.: "Nobody inspects".encode('utf-8')
    2. Use binary strings as shown in the manuals:

      m.update(b"Nobody inspects")
      m.update(b" the spammish repetition")
      

    The reason for the differing behaviour in the script to the shell is that the script stops on the error whereas in the shell the last line is a separate command but still not doing what you wish it to because of the previous error.

提交回复
热议问题