如何将每个经度、纬度和海拔高度保存到termin上运行的python的csv文件中

2024-05-29 02:32:13 发布

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

当前只保存python运行到csv文件时记录的最后一个经纬度和高度。但我需要把它们都保存到文件里。有什么想法吗?欢呼

def GPSPositionChanged(e):
    source = e.device
    print("%f, %f, %f") %(e.latitude, e.longitude, e.altitude)
    w=csv.writer(file(r'test6.csv','wb'), delimiter=';')
    some_values=[(e.latitude,e.longitude,e.altitude)]
    w.writerows(some_values)

这是完整的代码

import sys
import csv
#Phidget specific imports
from Phidgets.PhidgetException import PhidgetException
from Phidgets.Devices.GPS import GPS
from Phidgets.Phidget import PhidgetLogLevel



#Create an accelerometer object
try:
gps = GPS()
except RuntimeError as e:
print("Runtime Exception: %s" % e.details)
print("Exiting....")
exit(1)

#Information Display Function
def displayDeviceInfo():
print("|------------|----------------------------------|--------------|-------           -----|")
print("|- Attached -|-              Type              -|- Serial No. -|-          Version -|")
print("|------------|----------------------------------|--------------|------------|")
  print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (gps.isAttached(),       gps.getDeviceName(), gps.getSerialNum(), gps.getDeviceVersion()))
print("|------------|----------------------------------|--------------|------------|")

#Event Handler Callback Functions
def GPSAttached(e):
attached = e.device
print("GPS %i Attached!" % (attached.getSerialNum()))

def GPSDetached(e):
detached = e.device
 print("GPS %i Detached!" % (detached.getSerialNum()))

def GPSError(e):
try:
source = e.device
print("GPS %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode,       e.description))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))

def GPSPositionChanged(e):
source = e.device
print("%f, %f, %f") %(e.latitude, e.longitude, e.altitude)
w=csv.writer(file(r'test6.csv','wb'), delimiter=';')
some_values=[(e.latitude,e.longitude,e.altitude)]
w.writerows(some_values)



def GPSPositionFixStatusChanged(e):
source = e.device
if e.positionFixStatus:
status = "FIXED"
else:
status = "NOT FIXED"
print("GPS %i: Position Fix Status: %s" % (source.getSerialNum(), status))

#Main Program Code

try:
#logging example, uncomment to generate a log file
#gps.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log")


gps.setOnAttachHandler(GPSAttached)
gps.setOnDetachHandler(GPSDetached)
gps.setOnErrorhandler(GPSError)
gps.setOnPositionChangeHandler(GPSPositionChanged)
gps.setOnPositionFixStatusChangeHandler(GPSPositionFixStatusChanged)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)

print("Opening phidget object....")

try:
gps.openPhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)

print("Waiting for attach....")

try:
gps.waitForAttach(10000)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
try:
gps.closePhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Exiting....")

不知道把线圈放在哪里


Tags: csvimportsourcedevicedefasexceptiondetails

热门问题