使用 Json Python 格式化 Websocket 数据

Formmating Websocket data with Json Python

我正在通过 websocket 从网站接收数据,但有时 websocket 在 {} 中输出为空。我怎样才能给出一个过滤器,以便我只得到非空数据的输出。 if data != '{}' 函数不起作用我该怎么做才能使该函数有效?

import websocket
import pprint
import json 

while True: 
   data= ws.fetch(dataset)
   if data != '{}':
      pprint.pprint(data)

检查数据长度

if len(data)>0

len(someDict) 输出字典中的项目数

首先你必须将“While”更改为“while”。

试试这个:

if data:
    pprint.pprint(data)