稍复杂的多维数组运算,循环等,用于计算数学坐标、长度、里程、方位角、转角等文本数据,输出xls。供日后查阅。
import math import numpy as np import sympy as sy import xlwt import pandas as pd with open("zx1.txt","r")as f: data=f.read().splitlines()#使用字符串的splitlines()函数可以去掉末尾的\n。一定要使用read()方法读取文本。 f.closed x=len(data) n=[] for i in range(-1,x-1,1): i=i+1 l=data[i].split(",") #print(l) m=l[0],l[1],l[2],l[3] n.append(m) n1=np.array(n) print(n1) c1 = [] a0 = n1[0] a1 = [a0[0], "0+000.00", "000 00 00", '%.3f' % float(a0[3]), '%.3f' % float(a0[2]), '%.3f' % float(a0[1])] c1.append(list(a1)) print(len(n1)) lc = 0.0 zj = 0.0 # 定义浮点里程变量s1 for j in range(0, len(n1) - 1, 1): dx = float(n1[j + 1][2]) - float(n1[j][2]) + 0.1 ** 20 dy = float(n1[j + 1][1]) - float(n1[j][1]) a = math.sqrt(dx ** 2 + dy ** 2) fwj = math.degrees(math.atan(abs(dy / dx))) if dx > 0 and dy > 0: fwj = fwj if dx < 0 and dy > 0: fwj = 180 - fwj if dx < 0 and dy < 0: fwj = 180 + fwj if dx > 0 and dy < 0: fwj = 360 - fwj lc = lc + a # 计算累积里程 # print('fwj:',j,fwj) if 0 < j + 2 <= len(n1) - 1: dx1 = float(n1[j + 2][2]) - float(n1[j + 1][2]) + 0.1 ** 20 dy1 = float(n1[j + 2][1]) - float(n1[j + 1][1]) fwj1 = math.degrees(math.atan(abs(dy1 / dx1))) if dx1 > 0 and dy1 > 0: fwj1 = fwj1 if dx1 < 0 and dy1 > 0: fwj1 = 180 - fwj1 if dx1 < 0 and dy1 < 0: fwj1 = 180 + fwj1 if dx1 > 0 and dy1 < 0: fwj1 = 360 - fwj1 # print('fwj1:',j,fwj1) zj = fwj1 - fwj # print('zj:',j,zj) if 180 <= zj < 360: zj = zj - 180 elif 180 > zj >= -180: zj = zj + 180 elif -180 > zj >= -360: zj = zj + 540 else: zj = zj + 0.00 zj1 = str(int(zj)).zfill(3) + " " + str(int((zj - int(zj)) * 60)).zfill(2) + " " + str(int( (((zj - int(zj)) * 60) - int((zj - int(zj)) * 60)) * 60)).zfill(2) if lc < 1000: lc_k = str(0) else: lc_k = str('%.2f' %lc)[:-6] c = [n1[j+1][0],lc_k+'+'+str('%.2f' %lc)[-6:],zj1,n1[j+1][3],n1[j+1][2],n1[j+1][1]] c1.append(c) c2=np.array(c1) print(c2) #将c2写入excel文件 writer = pd.ExcelWriter('zxcgb.xlsx') data_1 = pd.DataFrame(c2,index=range(1,len(c2)+1),columns=['桩号', '里程\n(km+m)', '转角\n(° ′ ″)','高程\n(m)','纵坐标X\n(m)','横坐标Y\n(m)']) data_1.to_excel(writer, 'sheet1') writer.save()