我有一个从Web界面提取json数据的应用程序。
在大多数情况下,数据都存在,但是偶尔会出现,或者多次返回同一件事。
我已经删除了新行并选择了第一行,并将其分配给变量。当我循环打印函数时,它仍会打印一些“无”。
如果我在函数中将返回值更改为“打印”,则它似乎仅打印有效数据,因此我对如何获得“无人值守”感到困惑。
功能
def getTest():
for o in getData():
if 'system' in o:
currentValue = o['tag']
try:
stripValue = currentValue.splitlines()[0] # grab first line and drop the rest
print(stripValue)
except:
pass
输出
Talkgroup ID 54638 [0xd56e]
Talkgroup ID 54638 [0xd56e]
Talkgroup ID 54638 [0xd56e]
Talkgroup ID 54638 [0xd56e]
Talkgroup ID 54638 [0xd56e]
Talkgroup ID 54638 [0xd56e]
Talkgroup ID 54638 [0xd56e]
带有“返回”的代码
def getTest():
for o in getData():
if 'system' in o:
currentValue = o['tag']
try:
stripValue = currentValue.splitlines()[0] # grab first line and drop the rest
return(stripValue)
except:
pass
以上功能的输出
Talkgroup ID 13570 [0x3502]
None
Talkgroup ID 13570 [0x3502]
Talkgroup ID 13570 [0x3502]
Talkgroup ID 13570 [0x3502]
None
Talkgroup ID 13570 [0x3502]
下面是getData函数
def getData(debug=False):
debug == False
data = [{'command': 'update', 'data': 0}]
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post("http://pi25.local", data=json.dumps(data), headers=headers)
if debug == True:
print(r.status_code, r.reason)
data = r.json()
return data
下面是“数据”的输出
请注意,这会更改,有时并非每次查询时所有变量都具有值。
[{'nac': 835, 'wacn': 781824, 'tgid': None, 'tdma': None, 'json_type': 'change_freq', 'system': 'P25 SYSTEM', 'sysid': 840, 'tag': '', 'offset': 0, 'stream_url': '', 'freq': 855862500, 'center_frequency': None, 'fine_tune': 0.0}, {'nac': 835, '835': {'rxchan': 855862500, 'txchan': 810862500, 'frequency_data': {'854887500': {'last_activity': ' 11.8', 'tgids': [13570, None], 'counter': 1577}, '855787500': {'last_activity': ' 5.0', 'tgids': [27672, None], 'counter': 1515}, '855412500': {'last_activity': ' 1.0', 'tgids': [13570, None], 'counter': 1729}, '854462500': {'last_activity': ' 26.9', 'tgids': [6531, None], 'counter': 1646}, '854437500': {'last_activity': ' 34.3', 'tgids': [27550, None], 'counter': 1448}, '854987500': {'last_activity': ' 14.4', 'tgids': [54638, None], 'counter': 1706}, '855462500': {'last_activity': ' 13.4', 'tgids': [27554, None], 'counter': 1729}, '852575000': {'last_activity': ' 14.6', 'tgids': [27500, None], 'counter': 1963}, '856212500': {'last_activity': ' 25.8', 'tgids': [54768, None], 'counter': 1510}, '856237500': {'last_activity': ' 31.6', 'tgids': [54768, None], 'counter': 1506}, '855937500': {'last_activity': ' 28.5', 'tgids': [27617, None], 'counter': 1774}, '854662500': {'last_activity': ' 0.4', 'tgids': [54768, None], 'counter': 1547}}, 'tsbks': 84694, 'wacn': 781824, 'frequencies': {'854887500': 'voice frequency 854.887500 tgid(s) 13570 None 11.8s ago count 1577', '855787500': 'voice frequency 855.787500 tgid(s) 27672 None 5.0s ago count 1515', '855412500': 'voice frequency 855.412500 tgid(s) 13570 None 1.0s ago count 1729', '854462500': 'voice frequency 854.462500 tgid(s) 6531 None 26.9s ago count 1646', '854437500': 'voice frequency 854.437500 tgid(s) 27550 None 34.3s ago count 1448', '854987500': 'voice frequency 854.987500 tgid(s) 54638 None 14.4s ago count 1706', '855462500': 'voice frequency 855.462500 tgid(s) 27554 None 13.4s ago count 1729', '852575000': 'voice frequency 852.575000 tgid(s) 27500 None 14.6s ago count 1963', '856212500': 'voice frequency 856.212500 tgid(s) 54768 None 25.8s ago count 1510', '856237500': 'voice frequency 856.237500 tgid(s) 54768 None 31.6s ago count 1506', '855937500': 'voice frequency 855.937500 tgid(s) 27617 None 28.5s ago count 1774', '854662500': 'voice frequency 854.662500 tgid(s) 54768 None 0.4s ago count 1547'}, 'stid': 3, 'rfid': 2, 'last_tsbk': 1558581997.944967, 'adjacent_data': {'774781250': {'table': 1, 'stid': 1, 'rfid': 2, 'uplink': 804781250}, '773556250': {'table': 1, 'stid': 8, 'rfid': 2, 'uplink': 803556250}, '856037500': {'table': 0, 'stid': 10, 'rfid': 2, 'uplink': 811037500}, '771181250': {'table': 1, 'stid': 9, 'rfid': 2, 'uplink': 801181250}}, 'sysid': 840, 'syid': 840, 'secondary': [860762500, 860812500, 860862500]}, 'encrypted': 0, 'json_type': 'trunk_update', 'srcaddr': 0, 'grpaddr': 13570}, {'fine_tune': 0.0, 'files': [], 'json_type': 'rx_update', 'error': 341}]
我找到了一种无需重复进行此操作的方法,但是我仍然会得到这个随机的None
def test():
try:
data = [{'command': 'update', 'data': 0}]
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post("http://pi25.local", data=json.dumps(data), headers=headers)
data = r.json()
return data[0]['tag']
except:
pass
while True:
print(test())