NameError:未定义名称“海拔”

时间:2018-08-28 14:42:45

标签: python

我有这个gps代码,它获取gps数据并将其写入日志:

#!/usr/bin/python

from systemd import journal
import gps
import time
import threading
import datetime

# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
    try:
        report = session.next() # Wait for a 'TPV' report and display 
the current time

        # To see all report data, uncomment the line below
        #print report

        if report['class'] == 'TPV':
            if hasattr(report, 'time'):
                timestamp = (time.time()*1000)
                #print timestamp

            if hasattr(report, 'lat'):
                latitude = report.lat
                #print latitude

            if hasattr(report, 'lon'):
                longitude = report.lon
                #print longitude    

            if hasattr(report, 'alt'):
                altitude = report.alt
                #print altitude

        else:
            timestamp = (time.time()*1000)
            latitude = 0
            longitude = 0 
            altitude = 0

        journal.send(
        channel = 'gps',
        priority = journal.Priority.INFO,
        timestamp = "%f" % (timestamp),
        latitude = "%f" % (latitude),
        longitude = "%f" % (longitude), 
        altitude = "%f" % (altitude), 
        )

except KeyError:
    pass
except KeyboardInterrupt:
    quit()
except StopIteration:
    session = None
    print "GPSD has terminated"

我收到此错误:

Traceback (most recent call last):
  File "gps-messi.py", line 57, in <module>
    altitude = "%f" % (altitude),
NameError: name 'altitude' is not defined

有趣的是,代码有时可以完美地工作,有时它会给我这个错误。我不知道该怎么做才能使其始终正常工作。与她的爆炸有关系吗?

5 个答案:

答案 0 :(得分:3)

根据情况,会话报告似乎没有名为“ altitude”的属性。如果存在属性“ alt”,它将创建一个可变高度,以使代码正常工作。如果属性“ alt”不存在,则代码将失败。在if语句之前初始化变量,它将正常工作。

答案 1 :(得分:3)

我的猜测是if hasattr(report, 'alt'):不能解析为True,然后

if hasattr(report, 'alt'):
    altitude = report.alt

不会为altitude分配任何内容,并且在那里您到达此处

journal.send(
    channel = 'gps',
    priority = journal.Priority.INFO,
    timestamp = "%f" % (timestamp),
    latitude = "%f" % (latitude),
    longitude = "%f" % (longitude), 
    altitude = "%f" % (altitude), 
    )

实际上,您在分配altitude之前就在使用它。为避免这种情况,您可以在达到这一点之前将altitude初始化为某个值

altitude=0

这样做将确保在使用它之前先对其进行分配,并且您不会遇到该错误。

答案 2 :(得分:2)

我同意@wesgur,您是在alititude状态下启动if,但是在某些情况下if语句的条件得不到满足,并且程序没有为{{ 1}}

答案 3 :(得分:1)

移动方块

    timestamp = (time.time()*1000)
    latitude = 0
    longitude = 0 
    altitude = 0

if report['class'] == 'TPV':分支之前。 因此,您将始终初始化四个变量。

答案 4 :(得分:0)

非常感谢大家。这就是我的代码现在的样子。我测试了它,看起来还不错。但是,如果我仍在某个地方滑倒,有人可以检查一下日记帐行的缩进吗?如果我所做的正确(我已经修改了一些变量的名称并提高了速度):

#!/usr/bin/python

from systemd import journal
import gps
import time
import threading
import datetime

# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
    try:
        report = session.next() # Wait for a 'TPV' report and display the current time  
        # To see all report data, uncomment the line below
        #print report

        timestamp = 0        
        latitude = 0
        longitude = 0 
        altitude = 0
        speed = 0

        if report['class'] == 'TPV':

            if hasattr(report, 'time'):
                timestamp = (time.time()*1000)
                #print timestamp

            if hasattr(report, 'lat'):
                latitude = report.lat
                #print latitude

            if hasattr(report, 'lon'):
                longitude = report.lon
                #print longitude  

            if hasattr(report, 'alt'):
                altitude = report.alt
                #print altitude

            if hasattr(report, 'speed'):
                speed = report.speed
                #print speed

        journal.send(
        channel = 'gps',
        priority = journal.Priority.INFO,
        timestamp = "%f" % (timestamp),
        lat_deg = "%f" % (latitude),
        lon_deg = "%f" % (longitude), 
        alt_m = "%f" % (altitude), 
        speed_mps = "%f" % (speed),
        )


    except KeyError:
        pass
    except KeyboardInterrupt:
        quit()
    except StopIteration:
        session = None
        print "GPSD has terminated"