AttributeError: ‘int‘ object has no attribute ‘encode‘

用python3执行python2的代码时提示“AttributeError: 'int' object has no attribute 'encode'“”错误

原脚本

        self.sk = SigningKey.from_pem(sk_pem)

        sk_hex = "".join(c.encode('hex') for c in self.sk.to_string())
        return default_sk.to_string() == self.sk.to_string()

原因是python3执行类型转换时与python2不同,如上,如果需要将c转换为hex类型以字符串表示,需用hex()方法

        hex语法:hex(x)

更改后的脚本

         self.sk = SigningKey.from_pem(sk_pem)

        sk_hex = "".join(hex(c) for c in self.sk.to_string())
        return default_sk.to_string() == self.sk.to_string()
 

 运行更改后脚本不再提示此错误


版权声明:本文为RENDONGY原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。