Python JSON加载文件错误

时间:2018-12-30 22:01:25

标签: python json python-3.x

我正在尝试将文件加载到变量,但是在json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)中收到错误whitelist = json.load(f),我在做什么错了?

def load_whitelist():
    global whitelist
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    if os.path.isfile(wl):
        with open(wl, mode='r') as f:
            whitelist = json.load(f)
            f.close()
            print(whitelist)

def save_whitelist():
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    if os.path.isfile(wl):
        with open(wl, mode='w') as f:
            json.dump(whitelist, f, sort_keys=False)
            f.close()

完整追溯:

Traceback (most recent call last):
  File "PC200.py", line 970, in <module>
    load_whitelist()
  File "PC200.py", line 51, in load_whitelist
    whitelist = json.load(f)
  File "C:\Users\Administrator\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\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Administrator\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\Administrator\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)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020022935828>

JSON

[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]

2 个答案:

答案 0 :(得分:1)

并不是真正的答案,但不能放入评论中。仅显示错误消息,就不可能知道会发生什么。可以使用一个空文件或不正确的编码来重现此错误。模拟空文件的示例:

>>> import json
>>> import io
>>> fd = io.StringIO()
>>> wl = json.load(fd)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    wl = json.load(fd)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Program Files (x86)\Python37-32\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)

模拟读取为Latin1的UTF16编码文件的示例:

>>> t = '''[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]'''
>>> tt = t.encode('utf16').decode('latin1')
>>> fd=io.StringIO(tt)
>>> wl = json.load(fd)
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    wl = json.load(fd)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Program Files (x86)\Python37-32\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)

区分问题的唯一方法是读取并打印文件内容:

with open(wl, mode='r') as f:
    data = f.read()                      # read file conten
    print(data)                          # display it as text
    print([hex(ord(i)) for i in data])   # and as an hex dump

这样,您将确定Python实际读取文件的方式。

答案 1 :(得分:0)

def load_whitelist():
    global whitelist
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    if os.path.isfile(wl):
        with open(wl, mode='r') as f:
            whitelist = json.load(f)
            f.close()

def save_whitelist():
    wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
    print(whitelist)
    with open(wl, mode='w') as f:
        json.dump(whitelist, f, sort_keys=False)
        f.close()

保存白名单似乎出现了错误,并且删除了JSON内容,代码本身还可以(我删除了if os.path.isfile(wl):,因为这是不必要的)。谢谢大家的帮助!