将字典值传递给哈希函数

时间:2019-08-04 18:58:39

标签: 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 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,字节或os.PathLike对象,而不是列表

如果我将调用命令更改为:

print(md5Checksum(**value))

我得到:

  

TypeError:**之后的md5Checksum()参数必须是映射,而不是列表”。

有人可以指出我所缺少的明显解决方法吗?

2 个答案:

答案 0 :(得分:2)

根据其余代码,您的value是文件名列表,而不是文件名。因此,您应该添加一个额外的循环。在将文件名添加到列表之前,先进行拟合 可能更好:

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)))

答案 1 :(得分:2)

open(filePath, 'rb'),期望filepath参数的类型为str,而在调用md5Checksum(value)时,您要向其传递一个类型为list的变量,因此可能应该添加循环遍历值中的项目,然后将这些项目传递给md5Checksum函数