Python 为什么不加载此json数据?

Python 为什么不加载此json数据?,python,python-3.x,Python,Python 3.x,尝试使用Json.loads从外部Json文件导入字典,但是当我这样做时,会遇到一条错误消息 import json import random winner = "None" winners = 0 class player: def __init__(self, playernum): self.playernum = playernum self.score = random.randint(1,6) self.name = "f

尝试使用Json.loads从外部Json文件导入字典,但是当我这样做时,会遇到一条错误消息

import json
import random

winner = "None"
winners = 0

class player:
    def __init__(self, playernum):
        self.playernum = playernum
        self.score = random.randint(1,6)
        self.name = "foo"
player1 = player(1)
player2 = player(2)

if player1.score > player2.score:
    winner = player1.name
    winners = player1.score
elif player2.score > player1.score:
    winner = player2.name
    winners = player2.score

with open("highscores.txt", "a") as f:
  json.dump({winner : winners}, f)
f.close()

with open("highscores.txt", "r") as f:
  scores = json.load(f)
f.close()

print(scores)
Highscores.txt目前包含7本字典;{“但以理”:71}{“约拿单”:26}{“约拿单”:58}{“但以理”:90}{“约拿单”:57}{“约拿单”:80}{“约拿单”:50}

它不应该打印出包含winner:winners字典的文件吗? 相反,我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/jonat/AppData/Local/Programs/Python/Python36-32/neacode.py", line 119, in <module>
    scores = json.load(f)

  File "C:\Users\jonat\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\jonat\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\jonat\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 15 (char 14)
回溯(最近一次呼叫最后一次):
文件“C:/Users/jonat/AppData/Local/Programs/Python/Python36-32/neacode.py”,第119行,在
scores=json.load(f)
文件“C:\Users\jonat\AppData\Local\Programs\Python\Python36-32\lib\json\\ uu_init\u_.py”,第299行,已加载
parse_常量=parse_常量,object_pairs_hook=object_pairs_hook,**千瓦)
文件“C:\Users\jonat\AppData\Local\Programs\Python\Python36-32\lib\json\\ u_init\u_.py”,第354行,加载
返回\u默认\u解码器。解码
文件“C:\Users\jonat\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py”,第342行,在decode中
raise JSONDecodeError(“额外数据”,s,结束)
json.decoder.JSONDecodeError:额外数据:第1行第15列(字符14)

为什么?

这是因为您将字典附加到文件中,这不是有效的json语法:

从io导入StringIO
导入json
s=''{“a”:1}
{“b”:2}
{“c”:3}''
fh=StringIO(s)
json.load(fh)#JSONDecodeError
此外,在append(
'a'
)模式下打开的文件中使用
json.dump
)不会包含换行符,这会产生更糟糕的非json。要处理此问题,我将更改附加到文件的方式:

打开('highscores.txt',a')作为fh的
:
obj=json.dumps({winner:winners})+'\n'#注意这里添加的换行符
fh.写入(obj)
这将允许您逐行读取文件:

打开('highscores.txt',r')作为fh的
:
分数=[fh中的行的json.load(line.strip())]
打印(分数)

另外,请注意,您不必调用
fh.close()
,因为
with
语句将为您解决这个问题

{“Daniel”:71}{“Jonathan”:26}{“Jonathan”:58}{“Daniel”:90}{“Jonathan”:57}{“Jonathan”:80}{“Jonathan”:50}是highscores.txt目前的样子请把它放在问题中。如果我想将变量写入文件并输出它们的值,我该如何使用此方法?@JWONTSEEIT我不确定我是否理解我尝试按照您的建议执行的问题,但我的代码仍然返回相同的错误消息啊,
json.dump
在为追加而打开的文件中不会包含换行符。我会编辑我的回答实际上没有必要,对吗?dumps将我的字典转换为字符串