[英]print(raw_message['altitude']) TypeError
I have tried on all forums how to fix this error, but I do not understand what I'm doing wrong.我已在所有论坛上尝试过如何修复此错误,但我不明白自己做错了什么。 If you can give me a hand you do me a big favor, thank you very much.如果你能帮我一把,你就帮我大忙了,非常感谢。 Want to clarify that I looked for solutions for all and this post is my last chance想澄清一下,我为所有人寻找解决方案,这篇文章是我最后的机会
Error output:错误输出:
Traceback (most recent call last):
File "test2.py", line 35, in <module>
client.run(callback=process_beacon, autoreconnect=True)
File "/Users/gionatadonati/Desktop/python-ogn-client/ogn/client/client.py", line 74, in run
callback(packet_str)
File "test2.py", line 29, in process_beacon
print(raw_message['altitude'])
TypeError: string indices must be integers
Code:代码:
from ogn.client import AprsClient
from ogn.parser import parse, ParseError
def inRange_square(s, minLat, maxLat, minLon, maxLon, minAlt, maxAlt):
if s.get('longitude', 0) < minLon or s['longitude'] > maxLon or s['latitude'] < minLat or s['latitude']>maxLat or s['altitude']<minAlt or s['altitude']>maxAlt :
return False
return True
def processPlane(plane):
#if(inRange_square(plane, 46.158593, 46.166797, 8.891647, 8.869160, 210, 1000)):
if(inRange_square(plane, 46.127356, 46.441491, 9.276551, 8.460816, 210, 1000)):
print("The plane is in range")
print('Received {raw_message}'.format(**beacon))
else:
#print("The plane is not in range")
'odd'
def process_beacon(raw_message):
try:
beacon = parse(raw_message)
#print('Received {raw_message}'.format(**beacon))
processPlane(beacon)
except ParseError as e:
print('Error, {}'.format(e.message))
print(raw_message['altitude'])
client = AprsClient(aprs_user='N0CALL')
client.connect()
try:
client.run(callback=process_beacon, autoreconnect=True)
except KeyboardInterrupt:
print('\nStop ogn gateway')
client.disconnect()
Output of print(raw_message)
: print(raw_message)
输出:
RND000000>APRS,qAS,EKHG:/131942h6505.31S/18136.75W^054/325/A=002591 !W31! idA4000000 +099fpm +1.8rot FL029.04 21.5dB 4e +1.6kHz gps11x17
You're trying to do你正在尝试做
print( raw_message['altitude'] )
and getting the error message并收到错误消息
string indices must be integers
So, the logical conclusions is that raw_message
is a String, not a dictionary.所以,合乎逻辑的结论是raw_message
是一个字符串,而不是一个字典。 You can use raw_message['altitude']
to get the altitude
key of a dictionary, but a String doesn't have keys - only indices.您可以使用raw_message['altitude']
来获取字典的altitude
键,但 String 没有键 - 只有索引。
Additionally, keep in mind where this error is occurring.此外,请记住发生此错误的位置。 It's inside of an except
block, and specifically only runs when you can't parse a raw message (and I'm assuming that the "raw message" is, indeed, just a regular string of text).它在一个except
块内,特别是仅在您无法解析原始消息时运行(我假设“原始消息”实际上只是一个常规的文本字符串)。
Try doing尝试做
print( raw_message )
instead for the debug information you need.而不是您需要的调试信息。 Or, at least, treat it as a string instead of as a dictionary.或者,至少,将其视为字符串而不是字典。 It's possible that the real cause of your error is that parse(raw_message)
is throwing an error and it's not supposed to, in which case you might want to do step-by-step debugging with a tool like PDB to figure out where it's failing.您的错误的真正原因可能是parse(raw_message)
抛出了一个错误,而它不应该如此,在这种情况下,您可能想要使用 PDB 之类的工具进行逐步调试以找出它失败的地方.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.