C:UserskaiDesktopCRC32-1-6位>python3 crc321-6.py verification checksum: 0x308e1d5b (OK) Traceback (most recent call last): File "C:UserskaiDesktopCRC32-1-6位crc321-6.py", line 3, incrc32_reverse(0x308e1d5b,4) File "C:UserskaiDesktopCRC32-1-6位my_crc32.py", line 129, in crc32_reverse obj.run_reverse() File "C:UserskaiDesktopCRC32-1-6位my_crc32.py", line 109, in run_reverse patch.extend(last_4_bytes) AttributeError: 'map' object has no attribute 'extend'
for item in self.dfs(self.length - 4):
patch = map(ord, item) ###Python 2.x 返回列表。
patches = self.findReverse(desired, self.calc(patch, accum))
for last_4_bytes in patches:
if all(p in self.char_set for p in last_4_bytes):
patch.extend(last_4_bytes)
print ('[find]: {1} ({0})'.format('OK' if self.calc(patch, accum) == desired else 'ERROR', ''.join(map(chr, patch))) )
这里就有一个问题,对于map()函数的用法,在python2和3中的有些许差别,也就导致无法后续继续调用计算:
map() 函数语法:
map(function, iterable, …)
参数
function – 函数
iterable – 一个或多个序列
返回值
Python 2.x 返回列表。
Python 3.x 返回迭代器。
所以需要把 patch = map(ord, item)修改成patch = list(map(ord, item) )
要把map函数返回的迭代器转换为列表(list),这就是由于python2,Python3版本不同偏差
for item in self.dfs(self.length - 4):
patch = list(map(ord, item) )### map(ord, item),Python 3.x 返回迭代器。
patches = self.findReverse(desired, self.calc(patch, accum))
for last_4_bytes in patches:
if all(p in self.char_set for p in last_4_bytes):
patch.extend(last_4_bytes)
print ('[find]: {1} ({0})'.format('OK' if self.calc(patch, accum) == desired else 'ERROR', ''.join(map(chr, patch))) )



