json 字符串卡住的 tqdm 进度条

tqdm progress bar with json string stuck

我有一个 json 字符串列表,我正在将它们转换为字典列表。

我这样做是为了将它们组合成一个最终的 json 字符串,稍后将转换为 Pandas 数据帧:

s1 = '{ "id": 11, "label": "REF", "claim": "Lorelai Gilmore", "ce": [[[1,2, "Gilmore", 3]]]}'
s2 = '{ "id": 0, "label": "REF", "claim": "named Robert s.", "ce": [[[1,2, "Lorelai", 3]]]}'
s = [s1, s2]

combine = [json.loads(item) for item in s]

r = json.dumps(combine, indent=2)
s = pandas.read_json(r)
print(s)

我手上的json个字符串列表很大,所以我尝试用Tqdm进度条来监控进度:

combine = tqdm([json.loads(item) for item in s])

但是我得到了这个错误:

  0%|          | 0/2 [00:00<?, ?it/s]Traceback (most recent call last):
  File "D:/OneDrive/PhD/fever_challenge/test.py", line 11, in <module>
    r = json.dumps(combine, indent=2)
  File "C:\Python35\lib\json\__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "C:\Python35\lib\json\encoder.py", line 200, in encode
    chunks = list(chunks)
  File "C:\Python35\lib\json\encoder.py", line 436, in _iterencode
    o = _default(o)
  File "C:\Python35\lib\json\encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError:   0%|          | 0/2 [00:00<?, ?it/s] is not JSON serializable

我在跟踪错误时删除了代码中的最后三行,我发现循环卡住了。这就是我看到的:

  0%|          | 0/2 [00:00<?, ?it/s]

我的代码有什么问题?

试试这个:

combine = []
for i in tqdm([json.loads(item) for item in s]):
    combine.append(i)