com*_*ous 5 python encryption json dictionary python-3.x
我得到了json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0),当我试图从我创建了一个JSON文件访问值。我运行了下面的runfile,似乎有这个解码器问题,但是,当创建json文件时,我将加密内容作为字符串添加到json文件的字典中。有人可以帮我找出问题所在吗?
错误是:
{
"youtube": {
"key": "D5IPLv06NGXut4kKdScNAP47AieP8wqeUINr6EFLXFs=",
"content": "gAAAAABclST8_XmHrAAfEbgrX-r6wwrJf7IAtDoLSkahXAraPjvoXeLl3HLkuHbW0uj5XpR4_jmkgk0ICmT8ZKP267-nnjnCpw=="
},
"facebook": {
"key": "YexP5dpgxwKhD8Flr6hbJhMiAB1nmzZXi2IMMO3agXg=",
"content": "gAAAAABclST8zSRen_0sur79NQk9Pin16PZcg95kEHnFt5vjKENMPckpnK9JQctayouQ8tHHeRNu--s58Jj3IPsPbrLoeOwr-mwdU5KvvaXLY-g6bUwnIp4="
},
"instagram": {
"key": "ew2bl0tKdlgwiWfhB0jjSrOZDb41F88HULCQ_21EDGU=",
"content": "gAAAAABclST8FKcZqasiXfARRfbGPqb3pdDj4aKuxeJoRvgIPbVIOZEa5s34f0c_H3_itv5iG1O7u8vvlT8lAPTgAp3ez8OBh4T2OfBG-ObljYmIt7exi0Q="
}
}
Traceback (most recent call last):
File "C:\Users\YOURNAME\Desktop\randomprojects\content_key_writer.py", line 65, in <module>
main()
File "C:\Users\YOURNAME\Desktop\randomprojects\content_key_writer.py", line 60, in main
data_content = json.load(data_file)
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Run Code Online (Sandbox Code Playgroud)
原来的代码贴在这里,命名为runfile:
{
"youtube": {
"key": "D5IPLv06NGXut4kKdScNAP47AieP8wqeUINr6EFLXFs=",
"content": "gAAAAABclST8_XmHrAAfEbgrX-r6wwrJf7IAtDoLSkahXAraPjvoXeLl3HLkuHbW0uj5XpR4_jmkgk0ICmT8ZKP267-nnjnCpw=="
},
"facebook": {
"key": "YexP5dpgxwKhD8Flr6hbJhMiAB1nmzZXi2IMMO3agXg=",
"content": "gAAAAABclST8zSRen_0sur79NQk9Pin16PZcg95kEHnFt5vjKENMPckpnK9JQctayouQ8tHHeRNu--s58Jj3IPsPbrLoeOwr-mwdU5KvvaXLY-g6bUwnIp4="
},
"instagram": {
"key": "ew2bl0tKdlgwiWfhB0jjSrOZDb41F88HULCQ_21EDGU=",
"content": "gAAAAABclST8FKcZqasiXfARRfbGPqb3pdDj4aKuxeJoRvgIPbVIOZEa5s34f0c_H3_itv5iG1O7u8vvlT8lAPTgAp3ez8OBh4T2OfBG-ObljYmIt7exi0Q="
}
}
Traceback (most recent call last):
File "C:\Users\YOURNAME\Desktop\randomprojects\content_key_writer.py", line 65, in <module>
main()
File "C:\Users\YOURNAME\Desktop\randomprojects\content_key_writer.py", line 60, in main
data_content = json.load(data_file)
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\YOURNAME\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Run Code Online (Sandbox Code Playgroud)
上面代码中引入的模块是encipher_decipher:
import sys
import os
from cryptography.fernet import Fernet
import json
import pathlib
from encipher_decipher import encrypt, decrypt, bytes_to_str, str_to_bytes
def content_key_writer(path, filename, account, content):
"""Generate key corresponding to an account, save in json"""
# make the path a Path object
path = pathlib.Path(path)
file_path = os.path.join(path, filename)
# generate a key using Fernet
key = Fernet.generate_key()
# json doesn't support bytes, so convert to string
key = bytes_to_str(key)
# with file_path, see if the file exists
if not os.path.exists(file_path):
# build the dictionary to hold key and content
data = {}
data[account] = {}
data[account]['key'] = key
data[account]['content'] = encrypt(content, key)
# if the file doesn't exist, build the new json file
with open(file_path, 'w') as f:
json.dump(data, f)
else:
# if the file does exist
with open(file_path, 'r') as f:
data = json.load(f)
data[account] = {} # <--- add the account
data[account]['key'] = key
data[account]['content'] = encrypt(content, key)
os.remove(file_path) # <--- remove the file and rewrite it
with open(file_path, 'w') as f:
json.dump(data, f, indent=4)
def main():
path = "C:/Users/YOURNAME/Desktop/randomprojects"
name = 'content.json'
account = 'youtube'
content = 'youtubepassword'
account2 = 'facebook'
content2 = 'facebookpassword'
account3 = 'instagram'
content3 = 'instagrampassword'
content_key_writer(path, name, account, content)
content_key_writer(path, name, account2, content2)
content_key_writer(path, name, account3, content3)
new_path = os.path.join(pathlib.Path(path),name)
with open(new_path) as data_file:
data = data_file.read()
print(data)
data_content = json.load(data_file)
value = data_content['youtube']['content']
print(value)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
Dan*_* M. 11
问题是这段代码:
with open(new_path) as data_file:
data = data_file.read()
print(data)
data_content = json.load(data_file)
Run Code Online (Sandbox Code Playgroud)
您正在将文件内容读入data,打印它,然后再次要求json.load()从文件句柄中读取。但是此时,文件指针已经在文件末尾,因此没有更多数据,因此出现 json 错误:Expecting value
改为这样做:
with open(new_path) as data_file:
data = data_file.read()
print(data)
data_content = json.loads(data)
Run Code Online (Sandbox Code Playgroud)
您已经将数据读入data,因此您只需将该字符串输入json.loads()
| 归档时间: |
|
| 查看次数: |
21766 次 |
| 最近记录: |