为什么我在运行raspberry pi的定位程序时会得到一个单词location的<class'keyrerror'>?

2024-04-26 09:57:03 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行这段python代码时,我遇到了一个问题:

    import sys
    import time

    import geolocation
    import publisher

    SleepTime = 10  # seconds
    _lat = 0.00
    _lon = 0.00


     def maintain():
         global _lat
         global _lon
         (lat, lon, accuracy) = geolocation.getLocation()
         if lat != _lat or lon != _lon:
            data = str(lat) + "," + str(lon) + "," + str(accuracy)
            print("publishing ", data)
            publisher.publishtoInternet(data)
            _lat = lat
            _lon = lon
        else:
            print("no change in coordinates")


    print("program begins")
    while True:
          try:
              maintain()
    except Exception as inst:
         print(type(inst), ' exception captured')
         print(inst)
         sys.stdout.flush()
         # file = open('/tmp/locktracker.error.log','a')
         # file.write('exception occurred, trying to reboot')
         # file.close()
         # call(["sudo","reboot"])
   # break
  for i in range(0, SleepTime):
      sys.stdout.write("\restarting in %d seconds " % (SleepTime - i))
      sys.stdout.flush()
      time.sleep(1)

使用这些称为geolocation和publisher的其他python脚本: 地理位置:

^{pr2}$

发布者:

    import time
    import urllib


    def publishtoInternet(temp):
        url = "https://api.thingspeak.com/update?api_key=" + temp
        try:
            print(url)
            result = urllib.urlopen(url).read()
            print(result + str(temp))
    except:
           print('exception uploading, logging to file')
           log(temp)


def log(temp):
    f = open('log', 'a')
    t = time.localtime(time.time())
    msg = str(t.tm_hour) + ' ' + str(t.tm_min)
    f.write(msg)
    f.write('temp: ')
    f.write(temp)

    f.write('\n')
    f.close()

我得到一个错误,对于位置词,我得到,然后代码重新启动。这真让我恼火,因为这是我学校的一个项目,我必须完成。把经度这个词删掉,把经度也删掉,把它删掉。在


Tags: importlogtimedefsystemppublisherfile