【发布时间】:2011-10-22 02:48:17
【问题描述】:
我正在尝试使用 python re 从文件中提取一些字符串,然后使用 MD5 对这个字符串进行 类似:
#MD5er.py
salt = extract_salt(file_foo)
print 'salt: %s' % salt
from md5 import md5
print 'hash: %s' % md5(salt).hexdigest()
$python MD5er
salt: \0001\072\206\277\354\107\134\061\361\076\150\047\010\124\200\315\100
hash: ce24166858853dfb12a86d7d602b0638
但是,像这样使用 iPython:
In [40]: salt = '\0001\072\206\277\354\107\134\061\361\076\150\047\010\124\200\315\100'
In [41]: salt
Out[41]: "\x001:\x86\xbf\xecG\\1\xf1>h'\x08T\x80\xcd@"
In [42]: print salt
1:���G\1�>hT��@
In [43]: from md5 import md5
In [44]: md5(salt).hexdigest()
Out[44]: 'ebae47a953591f7448ff7079837fb534'
任何线索为什么 MD5 在 2 个场景中不同? 为什么在 ipython 中当我输入变量名时,它以与原始字符串不同的格式出现,而 print() 输出是第三种格式!?
提示:
In [53]: import sys
In [54]: sys.getdefaultencoding()
Out[54]: 'ascii'
【问题讨论】:
-
文件中真的有反斜杠吗?
标签: python unicode hash md5 ascii