关于python:尝试将两个字典加载为JSON文件,但无法加载到第二个文件中

Trying to load in a two dictionaries as JSON files but cant load in the second file

我有两个JSON文件,我正试图在python中加载它们。

JSON

1
2
3
4
5
6
7
[
    ["Person 1","B"],
    ["Person 2","C"],
    ["Person 3","A"],
    ["Person 4","B"],
    ["Person 5","C"],
]

安德森

1
2
3
4
5
 {
  "A": 5,
  "B": 3,
  "C": 10
 }

我正在使用此代码加载到股票文件中

1
2
3
4
 import json

 # Load the stock file.
 stock = json.load(open("stock.json"))

但当我使用此代码加载到队列文件中时,它说不能对JSON对象进行编码:

1
2
3
4
 import json
 # Load the queue file.

 queue = json.load(open("queue.json"))


This is not valid json:

1
2
3
4
5
6
7
[
    ["Person 1","B"],
    ["Person 2","C"],
    ["Person 3","A"],
    ["Person 4","B"],
    ["Person 5","C"],
]

问题是列表中的尾随逗号。一种解决方案是使用yaml代替。yamljson的超集,接受逗号。

1
2
import yaml
queue = yaml.load(open(""queue.json"))


问题在于JSON文件的内容。尝试将queue.json更改为

[cc lang="python"][
{"B":"Person 1