简体   繁体   English

更新 Python 字典中的值

[英]Updating Values in Python Dictionaries

I am trying to update a key value in a dictionary but can't seem to get it working.我正在尝试更新字典中的键值,但似乎无法正常工作。 This takes values from a CSV file, hashes them, and creates a dictionary.这会从 CSV 文件中获取值,对它们进行哈希处理,然后创建一个字典。 The dictionary is then used to lookup and see if the main keys are in a JSON file.然后使用字典查找主键是否在 JSON 文件中。 If they are it should update the dictionary to show whether the value was found or not.如果是,则应更新字典以显示是否找到了该值。

import csv
import json
import hashlib
import xlwings as xw

# Set Excel file this is being called from to get the filename of the JSON file to check
wb = xw.Book('MyExcelFile.xlsx')
sht = wb.sheets('Sheet1')
filename = r'C:\PycharmProjects\MyJSONFile.json'

# Set CSV file to read from
my_file = open('testdata.csv', 'r')
reader = csv.reader(my_file)

# Declare the dictionary variable
my_dict = {}

# Read the value to hash from the file
for row in reader:
    #row[0] will be the value to hash - it's actually the column number
    original_value = row[0].removeprefix('Val:')
    hash_value = original_value

    # Hash the value
    i = 0
    while i < 5000:
        hash_value = hashlib.sha512(str(hash_value).encode("utf-8")).hexdigest()
        i += 1

    # Add the original value, status, and hashed value to the dictionary
    my_dict[hash_value] = {'Status': row[1], 'Original Value': original_value}

# Open the JSON file
with open(filename) as my_JSON:
    data = json.load(my_JSON)

# THIS IS THE BIT I NEED TO GET WORKING - IT SHOULD UPDATE THE 'Status' KEY IF IT FINDS THE VALUE IN THE JSON FILE
for key in my_dict:
    if key in data["skrotyPodatnikowCzynnych"]:
        print('Found it: ' + key)
        my_dict[hash_value]['Status'] = 'This has been found'
        print(my_dict[hash_value]['Status'])
    else:
        print('Not found: ' + key)
        my_dict[hash_value]['Status'] = 'This is missing'
        print(my_dict[hash_value]['Status'])

print(my_dict)

Everything works fine until trying to update the dictionary after the JSON lookup.一切正常,直到在 JSON 查找后尝试更新字典。 The results show this below, but when I run the final "print(my_dict)" line of code it hasn't updated the dictionary as the 'Status' field is still set to it's original value of 'Not Checked'.结果如下所示,但是当我运行最后的“print(my_dict)”代码行时,它没有更新字典,因为“状态”字段仍设置为其原始值“未检查”。 I need to update the 'Status' field to show whether the 'hash_value' was found so that I can then pass it back to Excel.我需要更新“状态”字段以显示是否找到了“hash_value”,以便我可以将其传回 Excel。

NOTE: I have abbreviated some of the values to protect the identity of the data.注意:我已经缩写了一些值以保护数据的身份。

Found it: 72cc2ecf0dc8dd7e7b56555f0ccacdde13b37
This has been found
Found it: a496101c748deca2128d6d9b82d35e1490ce6
This has been found
Found it: 21e80e863969b2fb230dadd6c301cabf3cdb1
This is missing

{'72cc2ecf0dc8dd7e7b56555f0ccacdde13b37': {'Status': 'Not Checked', 'Original Value': '2021060911330097'}, {'72cc2ecf0dc8dd7e7b56555f0ccacdde13b37': {'Status': 'Not Checked', 'Original Value': '202106091133009779'}, {'72cc2ecf0dc8dd7e7b56555f0ccacdde13b37': {'Status': 'Not Checked', 'Original Value': '20210609113300977978249'}}

Thank you.谢谢你。

It should be它应该是

# THIS IS THE BIT I NEED TO GET WORKING - IT SHOULD UPDATE THE 'Status' KEY IF IT FINDS THE VALUE IN THE JSON FILE
for key in my_dict:
    if key in data["skrotyPodatnikowCzynnych"]:
        print('Found it: ' + key)
        my_dict[key]['Status'] = 'This has been found' # modify hash_value to key
        print(my_dict[key]['Status'])
    else:
        print('Not found: ' + key)
        my_dict[key]['Status'] = 'This is missing' # modify hash_value to key
        print(my_dict[key]['Status'])

print(my_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM