首页 > 解决方案 > 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)

我找不到解决方案,任何帮助将不胜感激,谢谢。

标签: jsonpython-3.x

解决方案


推荐阅读