print(raw_message ['altitude'])TypeError

时间:2018-07-13 13:29:30

标签: python

我在所有论坛上都尝试过解决该错误的方法,但是我不明白自己在做什么错。 如果您能帮我一个忙,请帮我一个大忙,非常感谢。 想要澄清一下,我正在寻找所有人的解决方案,这篇文章是我最后的机会

错误输出:

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

代码:

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()

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

1 个答案:

答案 0 :(得分:1)

您正在尝试

print( raw_message['altitude'] )

并获取错误消息

string indices must be integers

因此,逻辑结论是raw_message是字符串,而不是字典。您可以使用raw_message['altitude']来获取字典的altitude键,但是String没有键-只有索引。

此外,请记住发生此错误的位置。它位于except块内部,并且仅在无法解析原始消息时才运行(并且我假设“原始消息”实际上只是一个常规的字符串)。

尝试做

print( raw_message )

代替您需要的调试信息。或者,至少将其视为字符串而不是字典。错误的真正原因很可能是parse(raw_message)抛出了一个错误,并且不应该这样做,在这种情况下,您可能希望使用PDB之类的工具进行逐步调试,以找出错误所在失败。