1
I need to generate a sha1 encryption of a string value from a JSON file, but I’m not getting it. Does anyone know where I’m going wrong with my code? The code opens the json file, takes the String that is in the field1 key transforms into sha1 and then writes the value to the field2 key, saves and closes the json file.
import requests
import json
import hashlib
arq = open('arq.json', 'r')
data = json.load(arq)
arq.close()
resumo = hashlib.sha1(data['campo1']).hexdigest()
data['campo2'] = resumo
arq = open('arq.json', 'w+')
arq.write(json.dumps(data))
arq.close()
Giving a Typeerror error: Unicode-Objects must be encoded before hashing
What’s going on? The code seems correct. If the external structure of your json is a list and not a dictionary, it will fail. As you will re-write the whole file, "w" mode, not "w+" should be used.
– jsbueno
is giving an Encode error
TypeError: Unicode-objects must be encoded before hashing
– Luciano Amaro
Ah - now it is possible to answer the question, without having to (1) write your code to a local program, (2) create an "out of the blue" example JSON that is compatible with the program and (3) run the program to see the error.
– jsbueno