json - python:无法从线程中的文件加载json数据
问题描述
所以,我正在开发一个 Steam 帐户生成器(现在是 GUI),但我遇到了一个问题。我有一个获取生成帐户的线程,URL
并且在某些时候,它调用一个函数来更改包含有关帐户信息URL
的文件中的值。JSON
当它尝试调用它时,我得到一个JSONDecodeError
,但是当我运行该函数来单独更改值时,它可以完美运行。
该线程正在主GUI
文件中运行,它正在调用另一个 py 文件,其中包含一些函数(steam_api_gandler_rewrite
文件),该changeJSONAccountValue
函数调用另一个 py 文件中的函数。
[线程(在主文件中)-> steam_api_handler_rewrite -> json_func]
加载文件和更改值的功能
import json
def changeJSONAccountValue(value, player, dict):
with open('files/accounts.json') as fp:
data = json.load(fp) ##<---- Loading JSON File
for acc in data['accounts']:
if acc['name'] == player:
acc[dict] = value
with open('files/accounts.json', 'w') as json_file:
json.dump(data, json_file, indent=2)
JSON:
{
"accounts": [
{
"name": "wTJxMKwhc",
"password": "",
"isBanned": false,
"lastBan": null,
"url": null
},
{
"name": "notfusion1",
"password": "",
"isBanned": false,
"lastBan": null,
"url": null
},
{
"name": "wGTwZGryG",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198918939167/"
},
{
"name": "KMylosPyc",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198920127567/"
},
{
"name": "gsxxMULlF",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198919031061/"
},
{
"name": "louuflNsD",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198919076337/"
},
{
"name": "mmidDeEMt",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198918842663/"
},
{
"name": "egDUDHNKg",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198920116444/"
},
{
"name": "EesxgxGkI",
"password": "",
"isBanned": false,
"lastBan": null,
"url": null
},
{
"name": "fJxYKgJZj",
"password": "",
"isBanned": false,
"lastBan": 0,
"url": "https://steamcommunity.com/profiles/76561198929144294/"
}
]
}
错误:
Exception in thread Thread-16:
Traceback (most recent call last):
File "C:\Users\Torre\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Torre\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Torre\PycharmProjects\SteamAccGenerator\Generator\steam_api_handler_rewrite.py", line 55, in update_ban_status
process_data(steam_player_bans_request(ids))
File "C:\Users\Torre\PycharmProjects\SteamAccGenerator\Generator\steam_api_handler_rewrite.py", line 72, in process_data
json_func.changeJSONAccountValue(player['VACBanned'], acc['name'], 'isBanned')
File "C:\Users\Torre\PycharmProjects\SteamAccGenerator\Generator\json_func.py", line 107, in changeJSONAccountValue
data = json.load(fp)
File "C:\Users\Torre\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Torre\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Torre\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Torre\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我找不到解决方案,任何帮助将不胜感激,谢谢。
解决方案
推荐阅读
- php - 我无法回显包含值的 JSON 编码对象
- node.js - 节点 msssql 本地服务器登录失败
- javascript - 如何使用 Express 路由器通过 index.js 查找函数(GET、POST、...)?
- reactjs - 无法在 Jest 中测试反应原生组件
- node.js - 从 eBay JSON 对象将数据分配给变量
- git - 对于 GitHub 提交,我作为共同作者出现
- openshift - 如何在 openshift 中使用正在运行的容器挂载网络共享(SAMBA Windows 共享)
- android - 如何重新生成一个apk?
- php - 处理由按钮组成的 html 表单
- amazon-web-services - DMS 终端节点是否支持 RDS IAM 身份验证?