import os, sys
import csv
import numpy as np
import math
csvFile = open("GPGGA.log", "r", newline='\n')
reader = csv.reader(csvFile)
data = []
for line in reader:
if(line[1] == 'gpsinfo'):
data.append(line[3:5])
data = np.array(data).astype("float64")
for i in range(len(data)):
for j in range(len(data[0])):
a, b = math.modf(data[i][j])
data[i][j] = b*100 + a*60
csvFile = open("data_convert.log", "w", newline='\n')
for i in range(len(data)):
things = '[ ' + str(float('%.6f'%data[i][0])) + ', ' + str(float('%.6f'%data[i][1])) + ' ],\n'
csvFile.write(things)
或者
import os, sys
import csv
import numpy as np
import math
path_abs=os.path.abspath('.')
logfile_path = open(path_abs + "\\path.log")
reader = csv.reader(logfile_path)
path = []
for line in reader:
path.append(''.join(line))
for i in range(len(path)):
path_temp = path_abs+path[i]
logfile_in = open(path_temp+'\\data.log', "r", newline='\n')
logfile_out = open(path_temp+"\\data_converted.log", "w", newline='\n')
reader = csv.reader(logfile_in)
data = []
for line in reader:
if(line[1] == 'gpsinfo'):
data.append(line[3:5])
data = np.array(data).astype("float64")
for i in range(len(data)):
for j in range(len(data[0])):
a, b = math.modf(data[i][j])
data[i][j] = b*100 + a*60
for i in range(len(data)):
lines = '[ ' + str(float('%.6f'%data[i][0])) + ', ' + str(float('%.6f'%data[i][1])) + ' ],\n'
logfile_out.write(lines)
或者使用Python内置函数 .strip() 和 .split()函数,进行分割比如:
由于在python3中map()函数的返回值类型为map object 并不是python2中的list因此需要进行list处理,如此可以将字符串转化为float型
def loadDataSet(fileName, delim='\t'):
fr = open(fileName)
stringArr = [line.strip().split(delim) for line in fr.readlines()]
datArr = [list(map(float, line)) for line in stringArr]
return mat(datArr)