Python 将字典值传递给哈希函数
我有一个字典,它的值包含文件名及其路径 我还有一个MD5hashing函数,我想用字典中的相关值调用它Python 将字典值传递给哈希函数,python,function,dictionary,Python,Function,Dictionary,我有一个字典,它的值包含文件名及其路径 我还有一个MD5hashing函数,我想用字典中的相关值调用它 with open('/tmp/foo') as csvfile: reader = csv.DictReader(csvfile) for row in reader: files[row['size']].append(row['file']) # print the contents of the files defaultdict dic
with open('/tmp/foo') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
files[row['size']].append(row['file'])
# print the contents of the files defaultdict dictionary
for key, value in files.items():
if len([item for item in value if item]) > 1:
print (key+'\n')
print(md5Checksum(value))
我的md5功能是:
def md5Checksum(filePath):
with open(filePath, 'rb') as fh:
m = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
当我把它称为“原样”时,我正在变得
TypeError:应为str、bytes或os.PathLike对象,而不是list
如果我将调用命令更改为:
print(md5Checksum(**value))
我得到:
TypeError:**后面的md5Checksum()参数必须是映射,而不是列表'
有人能指出我缺少的明显的修复吗?根据代码的其余部分,您的
值是一个文件名列表,而不是一个文件名。因此,您应该添加一个额外的循环。在将文件名添加到列表之前进行筛选也可能更好:
with open('/tmp/foo') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['file']: # preventing adding the file in the first place
files[row['size']].append(row['file'])
for key, values in files.items():
for value in values:
print ('{}\n{}'.format(key, md5Checksum(value)))
以open('/tmp/foo')作为csvfile的:
reader=csv.DictReader(csvfile)
对于读取器中的行:
如果行['file']:#首先阻止添加文件
文件[行['size']]。追加(行['file'])
对于键,文件中的值。项()
对于值中的值:
打印({}\n{}.format(key,md5Checksum(value))
open(filePath,'rb')
,期望filePath
参数为str类型,而调用md5Checksum(value)
时,您正在向它传递一个类型为list的变量,因此您可能应该在value中添加一个迭代项的循环,并将这些项传递给md5Checksum
函数值
这里是文件名列表,而不是文件名。