tqdm进度栏带有json字符串卡住

时间:2018-08-02 10:36:36

标签: json python-3.x pandas tqdm

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

我这样做是为了将它们合并到一个最终的json字符串中,稍后再转换为Pandas Dataframe:

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]

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

尝试一下:

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