首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError:未定义名称“”altitude“”

NameError:未定义名称“”altitude“”
EN

Stack Overflow用户
提问于 2018-08-28 22:42:45
回答 5查看 798关注 0票数 1

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

代码语言:javascript
复制
#!/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"

我得到了这个错误:

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

有趣的是,代码有时工作得很好,有时会给我这个错误。我不明白我应该怎么做才能让它一直正确地工作。是不是跟她的大爆炸有关?

EN

Stack Overflow用户

发布于 2018-08-29 00:13:36

非常感谢大家。这就是我的代码现在的样子。我测试了一下,它看起来没问题。但是,如果我仍然滑到某个地方,有人能检查一下行journal.send的缩进吗?如果我所做的是正确的(我已经修改了一些变量的名称并增加了速度):

代码语言:javascript
复制
#!/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"
票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52060580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档