0
我已经编写了下面的代码我能够打印出Lat和lon的解析值,但我无法将它们写入文件。我试过冲洗,也试图关闭文件,但没用。有人可以指出这里有什么错。无法使用Python 2.7写入文件
import os
import serial
def get_present_gps():
ser=serial.Serial('/dev/ttyUSB0',4800)
ser.open()
# open a file to write gps data
f = open('/home/iiith/Desktop/gps1.txt', 'w')
data=ser.read(1024) # read 1024 bytes
f.write(data) #write data into file
f = open('/home/iiith/Desktop/gps1.txt', 'r')# fetch the required file
f1 = open('/home/iiith/Desktop/gps2.txt', 'a+')
for line in f.read().split('\n'):
if line.startswith('$GPGGA'):
try:
lat, _, lon= line.split(',')[2:5]
lat=float(lat)
lon=float(lon)
print lat/100
print lon/100
a=[lat,lon]
f1.write(lat+",")
f1.flush()
f1.write(lon+"\n")
f1.flush()
f1.close()
except:
pass
while True:
get_present_gps()
'除了:pass'不能修复错误;它只是让Python停止告诉你他们。如果你不使用它,Python会给你一些关于你做错事情的有用信息。 – user2357112
你能澄清你的意思吗“无法写入文件”?当你运行你当前的代码时发生了什么?你预期会发生什么?如果你删除了'try'和'except'块,你会看到什么异常? – Blckknght