【问题标题】:Doing simple comparison in dictionary with Python用 Python 在字典中做简单的比较
【发布时间】:2020-05-12 02:29:23
【问题描述】:
import hashlib
import csv
with open ('C:\Python\Test\Hash.csv') as hash_file:
    # file_reader = csv.DictReader(hash_file)
    file_reader = csv.reader(hash_file)
    # counter = 0
    mydict = dict(filter(None,csv.reader(hash_file)))
    for i in range(1000,10000):
        bank_of_hash = hashlib.sha256(str(i).encode()).hexdigest()
        bank_of_hash={i:bank_of_hash}
    for counter in range(1,11):
        # counter +=1
        if mydict.values() == bank_of_hash.values():     
           print("This is your %s hash and this is the $a your decode" %(i,row))
        else:
            print("Password Not Found!!!")

大家好,首先感谢您对我的帮助。其次,就是这么简单,不知道为什么不行。

1-我导入库。 2-将其打开为 hash_file 3-这是我的 hash.csv 文件

  • 贾迪,9553627933b214db60798fe40d2b4f8497781d024f53d62dc1b12469b7d53784 贾法尔,58763bceaddcad6777063dd590cb8e50211b6bb7a11272110fef4060142ded20 乔布拉,08f88745513481cad0adf1ae0b225d6167ec690c2bc6d38558cb8ae399ce3bdb 彼得,99b057c8e3461b97f8d6c461338cf664bc84706b9cc2812daaebf210ea1b9974 诺鲁兹,996d7e2bc68410d18cf83438acaf9f8c81db7f7e4dd1c1116b52a1da57beaee3 山姆,d70ada757917455ce5a436e921854e35871e9e368050c3681c94ca9435c71c66 MahdimRa,1076c5957d3ea9ffad332d1a3785b90bc658dab676c9ba822c8fe55a3e6c23b4 萨吉,2876370ec5a463a2b84a7512e3dae1229460b5fe6c31b83ffabf2be977f7247b 纳维德,85432a9890aa5071733459b423ab2aff9f085f56ddfdb26c8fae0c2a04dce84c

4- 我阅读了 CSV 文件,然后将其转换为字典 5- 我知道我的哈希值是 1000-10000 6-用键和值制作一个哈希字典 7-我唯一想做的就是在两个字典之间进行简单的比较。

【问题讨论】:

  • 你不能这样比较。即使对于同一个字典 test= {'a':'1','b':'2'} test.values() == test.values() 返回 false
  • 我以为我可以进行这种类型的比较,然后我看到 Giannis Clipper 这样做了:for key in hash_data: if hash_data[key] == bank_of_hash:

标签: python dictionary encryption hash comparison


【解决方案1】:

我尝试运行您的代码,但无法将数据从哈希文件解析到字典,因此我将代码修改为这样并正常工作:

import hashlib

hash_data = dict()

with open ('C:\Python\Test\Hash.csv') as hash_file:
    for line in hash_file.read().split():
        hash_data[line.split(',')[0]] = line.split(',')[1]

for i in range(1000,10000):
    bank_of_hash = hashlib.sha256(str(i).encode()).hexdigest()

    for key in hash_data:
        if hash_data[key] == bank_of_hash:
            print(key, ':', i)

【讨论】:

  • 非常感谢它现在可以工作了。所以你删除了 file_reader = csv.reader(hash_file) 就不需要打开那个 CSV 了吗?那么这行是做什么的:hash_data[line.split(',')[0]] = line.split(',')[1] 我知道你将 hash_data 定义为字典但不理解这一行!
  • with open ('C:\Python\Test\Hash.csv') as hash_file: => 将 csv 文件作为文本文件打开。 hash_file.read() => 读取打开文件的内容。 hash_file.read().split() => 将该内容逐行解析为一个数组。 line.split(',')[0] => 将一行的内容解析为一个列表并获取第一个元素,即名称。 line.split(',')[1] => 将一行的内容解析为一个列表并获取第二个元素,即哈希字符串。 hash_data[line.split(',')[0]] = line.split(',')[1] => 以这种方式填充字典: hash_data['name'] = 'hash_string_here_as_dictionary_value'
猜你喜欢
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 2014-10-29
相关资源
最近更新 更多