这是页面源代码中的一些JSON:

match_info  = JSON.parse('\x7B\x22id\x22\x3A\x2211768\x22,\x22fid\x22\x3A\x221376025\x22,\x22h\x22\x3A\x2272\x22,\x22a\x22\x3A\x2279\x22,\x22date\x22\x3A\x222019\x2D11\x2D23\x2015\x3A00\x3A00\x22,\x22league_id\x22\x3A\x221\x22,\x22season\x22\x3A\x222019\x22,\x22h_goals\x22\x3A\x220\x22,\x22a_goals\x22\x3A\x222\x22,\x22team_h\x22\x3A\x22Everton\x22,\x22team_a\x22\x3A\x22Norwich\x22,\x22h_xg\x22\x3A\x220.812693\x22,\x22a_xg\x22\x3A\x221.80849\x22,\x22h_w\x22\x3A\x220.1424\x22,\x22h_d\x22\x3A\x220.2108\x22,\x22h_l\x22\x3A\x220.6468\x22,\x22league\x22\x3A\x22EPL\x22,\x22h_shot\x22\x3A\x2218\x22,\x22a_shot\x22\x3A\x2213\x22,\x22h_shotOnTarget\x22\x3A\x227\x22,\x22a_shotOnTarget\x22\x3A\x225\x22,\x22h_deep\x22\x3A\x2215\x22,\x22a_deep\x22\x3A\x224\x22,\x22a_ppda\x22\x3A\x228.2174\x22,\x22h_ppda\x22\x3A\x225.9565\x22\x7D');


尝试使用json.loads()方法将其转换为字典时,出现以下错误:

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    json.loads(shots)
  File "C:\Users\turnc\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\turnc\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\turnc\AppData\Local\Programs\Python\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)


使用双引号也无济于事。

如何在Python中将此字符串转换为字典?

最佳答案

尝试以下操作-使用bytes将其转换为b string prefix类型的对象:

>>> json.loads(b'\x7B\x22id\x22\x3A\x2211768\x22,\x22fid\x22\x3A\x221376025\x22,\x22h\x22\x3A\x2272\x22,\x22a\x22\x3A\x2279\x22,\x22date\x22\x3A\x222019\x2D11\x2D23\x2015\x3A00\x3A00\x22,\x22league_id\x22\x3A\x221\x22,\x22season\x22\x3A\x222019\x22,\x22h_goals\x22\x3A\x220\x22,\x22a_goals\x22\x3A\x222\x22,\x22team_h\x22\x3A\x22Everton\x22,\x22team_a\x22\x3A\x22Norwich\x22,\x22h_xg\x22\x3A\x220.812693\x22,\x22a_xg\x22\x3A\x221.80849\x22,\x22h_w\x22\x3A\x220.1424\x22,\x22h_d\x22\x3A\x220.2108\x22,\x22h_l\x22\x3A\x220.6468\x22,\x22league\x22\x3A\x22EPL\x22,\x22h_shot\x22\x3A\x2218\x22,\x22a_shot\x22\x3A\x2213\x22,\x22h_shotOnTarget\x22\x3A\x227\x22,\x22a_shotOnTarget\x22\x3A\x225\x22,\x22h_deep\x22\x3A\x2215\x22,\x22a_deep\x22\x3A\x224\x22,\x22a_ppda\x22\x3A\x228.2174\x22,\x22h_ppda\x22\x3A\x225.9565\x22\x7D')
{'id': '11768',
 'fid': '1376025',
 'h': '72',
 'a': '79',
 'date': '2019-11-23 15:00:00',
 'league_id': '1',
 'season': '2019',
 'h_goals': '0',
 'a_goals': '2',
 'team_h': 'Everton',
 'team_a': 'Norwich',
 'h_xg': '0.812693',
 'a_xg': '1.80849',
 'h_w': '0.1424',
 'h_d': '0.2108',
 'h_l': '0.6468',
 'league': 'EPL',
 'h_shot': '18',
 'a_shot': '13',
 'h_shotOnTarget': '7',
 'a_shotOnTarget': '5',
 'h_deep': '15',
 'a_deep': '4',
 'a_ppda': '8.2174',
 'h_ppda': '5.9565'}


或使用results变量中的字符串:

>>> json.loads(bytes(result, 'utf-8'))
{'id': '11768',
 'fid': '1376025',
 'h': '72',
 'a': '79',
 'date': '2019-11-23 15:00:00',
 'league_id': '1',
 'season': '2019',
 'h_goals': '0',
 'a_goals': '2',
 'team_h': 'Everton',
 'team_a': 'Norwich',
 'h_xg': '0.812693',
 'a_xg': '1.80849',
 'h_w': '0.1424',
 'h_d': '0.2108',
 'h_l': '0.6468',
 'league': 'EPL',
 'h_shot': '18',
 'a_shot': '13',
 'h_shotOnTarget': '7',
 'a_shotOnTarget': '5',
 'h_deep': '15',
 'a_deep': '4',
 'a_ppda': '8.2174',
 'h_ppda': '5.9565'}

09-27 00:44