import pynmea2 text = "$GNRMC,074733.00,A,2241.28818,N,11358.44210,E,3.866,,070720,,,A*60" msg = pynmea2.parse(text) print(msg.latitude) print(msg.longitude)
解析多個數據:
import pynmea2 #該函數主要針對整個文件的gps進行解析 def parse_file(file_path): txt_tables = [] f = open(file_path, "r",encoding='utf-8') line = f.readline() # 讀取第一行 locations = [] while line: text = line[1:] msg = pynmea2.parse(text) # print(msg.latitude) #24.551053333333332 # print(msg.longitude) #118.1067375 tmp = [] if(msg.latitude == 0.0 or msg.longitude == 0.0): line = f.readline() # 讀取下一行 continue tmp.append(msg.latitude) tmp.append(msg.longitude) locations.append(tmp) line = f.readline() # 讀取下一行 return locations if __name__ == '__main__': file_path ='gps_data_60.txt' locate = parse_file(file_path) print(locate)
附件,gps文件數據:gps_data_60.txt
$GNRMC,023445.200,A,3448.72275,N,11331.95121,E,0.00,222.94,110424,,,A*7A $GNRMC,023445.400,A,3448.72275,N,11331.95121,E,0.00,222.94,110424,,,A*7C $GNRMC,023445.600,A,3448.72275,N,11331.95121,E,0.00,222.94,110424,,,A*7E $GNRMC,023455.600,A,3448.72279,N,11331.95124,E,0.00,222.94,110424,,,A*76 $GNRMC,023455.800,A,3448.72279,N,11331.95124,E,0.00,222.94,110424,,,A*78 $GNRMC,023456.000,A,3448.72279,N,11331.95124,E,0.00,222.94,110424,,,A*73