我有以下的代码片段属性错误 - Python的
thetype = raw_input("Please enter hash type. md5 or sha1")
hash_type = hashlib.thetype(word).hexdigest()
这将返回错误“AttributeError的:‘模块’对象有没有属性‘thetype’ ”我有点明白为什么,但我想我是什么真正问的是,我该如何解决这个问题?
我有以下的代码片段属性错误 - Python的
thetype = raw_input("Please enter hash type. md5 or sha1")
hash_type = hashlib.thetype(word).hexdigest()
这将返回错误“AttributeError的:‘模块’对象有没有属性‘thetype’ ”我有点明白为什么,但我想我是什么真正问的是,我该如何解决这个问题?
通过使用字典(您也可以使用getattr
,但它引入了获取其他不相关属性的可能性)。
d = {"md5" : hashlib.md5, "sha1" : hashlib.sha1}
hash_type = raw_input("Please enter hash type. md5 or sha1")
d[hash_type].hexdigest()
此外,raw_input
已经返回str
,所以没有必要再打电话str
。
非常感谢!完美工作。 – user1448015 2012-07-26 02:30:44
是的,我知道这件事情。我正在搞砸哈哈。甚至没有意识到我忘了删除它 – user1448015 2012-07-26 02:44:39
import hashlib
thetype = raw_input("Please enter hash type. %r"%(hashlib.algorithms,))
# 2.7 or later... just catch the exception from .new(thetype) for older versions.
if thetype in hashlib.algorithms:
print hashlib.new(thetype)('some data').hexdigest()
else:
print "No Way!"
只需使用hashlib.new(type,word).hexdigest() – gps 2012-07-27 00:12:10