def JudgeTwoWay(wayid1, wayid2): """ 判断两个相邻有交点路段是否能够互通,即从way1 是否能到达way2 注意:此部分只判断有交点的两个路段 没有交点的两个路段会直接判断为不能互通 :param wayid1: :param wayid2: :return: """ if wayid1 == wayid2: return True node_id = TwoWay_intersection(wayid1, wayid2) # 两条路段交点 #print(node_id) if not node_id: #两个路段没有交点 return False wayslist1 = Get_way_NodeID(wayid1) wayslist2 = Get_way_NodeID(wayid2) # 示例:[320524866, 2207731964, 320524867] index = wayslist2.index(node_id[0][0]) # node_id[0][0]为取出交点 node_id为嵌套元组 if index == len( wayslist2) - 1: # 交点是way2的最后一个点,那么即使way1 way2有交点,则way1也是无法到达way2的 Common_Functions.SaveRoutesConn("connects", wayid1, wayid2, 0) return False elif wayslist1.index(node_id[0][0]) == 0 and wayslist2.index( node_id[0][0]) == 0: #交点同时是way1 way2的第一个点 return True elif wayslist1.index(node_id[0][0]) == 0: # 交点是way1的第一个点 Common_Functions.SaveRoutesConn("connects", wayid1, wayid2, 0) return False else: Common_Functions.SaveRoutesConn("connects", wayid1, wayid2, 1) return True
def FinalLines(txtpath,kmlsavepath,txtfile): """ 批量处理每辆车的完成路网匹配 :param txtpath: txt文件路径,txt文件是最终确定的轨迹点所属路段的存储文件 :param kmlsavepath: kml文件的保存路径 txtfile kml路线对应的txt文档 :return: """ if not os.path.isdir(kmlsavepath): os.mkdir(kmlsavepath) with open(txtpath,'r') as file: lines = file.readlines() linesnum = len(lines) finalconnways = [] #最终能走通的路线 count = 0 for i in range(linesnum): waylists = GetAllLines(eval(lines[i].strip("\n"))) if len(waylists)>=3: if MapNavigation.JudgeLines(waylists): finalconnways.append(waylists) elif len(waylists)==2: if MapNavigation.JudgeTwoWay(waylists[0],waylists[1]): finalconnways.append(waylists) else:pass #print(finalconnways) finalconnways = Common_Functions.Double_layer_list(finalconnways) file = open(txtfile,'a') for sub in finalconnways: print(sub) file.write(str(sub)+"\n") count += 1 nodes = Fill_coordinate_By_Routes(sub) Common_Functions.list2kml(nodes, str(count),kmlsavepath) file.close()
def BatchSelectLines(Candidatewaypath,savepath): """ 批量选出最终匹配的轨迹 :param Candidatewaypath: 所有候选路线的txt路径 :param savepath: kml保存路径 :return: """ txtlist = Common_Functions.findtxtpath(Candidatewaypath) for singletxt in txtlist: print(singletxt) (tempath, tempfilename) = os.path.split(singletxt) # tempfilename为txt文件名(包含后缀) (trunkname, extension) = os.path.splitext(tempfilename) # filename 为传入的txt文件名 extension为后缀 kmlsavepath = os.path.join(savepath,trunkname) txtkmllinename = trunkname +".txt" if not os.path.isdir(kmlsavepath): os.mkdir(kmlsavepath) FinalLines(singletxt,kmlsavepath,os.path.join(kmlsavepath,txtkmllinename))
def Nodirectionwaytoway(way_id1, way_id2, max_sum=8): """ 无方向 有交点就会认为通行 :param way_id1: :param way_id2: :param max_sum: :return: """ if way_id1 == way_id2: return [way_id1, way_id2] finalroute = [] node_id = TwoWay_intersection(way_id1, way_id2) #两条路段交点 if node_id: if JudgeTwoWay(way_id1, way_id2): finalroute.extend([way_id1, way_id2]) Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 1) return finalroute else: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False else: #两条路段没有直接交点 Candidate_Routes = [[way_id1]] # 候选路线 flag = 0 count = 0 #迭代多少次之后仍然没有找到可行路线,则认为不可走 exitflag = 0 #标记是否是通过找到满足条件的路线而退出的 grid1 = Getway_startendnode_grid(way_id1, flag=0) startx = grid1[0] starty = grid1[1] grid2 = Getway_startendnode_grid(way_id1, flag=0) Endx = grid2[0] Endy = grid2[1] while True: temCandidate_Routes = [] # 存储当前这一轮新的候选路线 AllNextways = [] MaxDel = [] #存因路段数目大于阈值,而筛除的路线 for sub in Candidate_Routes: if way_id2 in sub: flag = 1 break if len(sub) > max_sum: #防止寻找时间过长,如果目的是为了查找路线,可将此条件删除 MaxDel.append(sub) for sub in MaxDel: Candidate_Routes.remove(sub) if len(Candidate_Routes) == 0: flag = 1 exitflag = 1 if count == 8: flag = 1 exitflag = 1 if flag == 1: break for subroute in Candidate_Routes: # subroute 表示正在处理的路线 preways = subroute #表示当前路线已有的路段 processingway = subroute[-1] # 表示要处理的路段 nextway = NodirectionFindNextWay(processingway, preways) # 下一步的可选路段 if len(nextway) == 0: #当前路线下一步没有能走的路 pass else: AllNextways.extend(nextway) for next in nextway: temroute = copy.deepcopy(subroute) temroute.append(next) temCandidate_Routes.append(temroute) count += 1 Candidate_Routes.clear() Candidate_Routes = temCandidate_Routes Candidate_Routes = Common_Functions.Double_layer_list( Candidate_Routes) Candidate_Routes = Common_Functions.Main_Auxiliary_road( Candidate_Routes) # 去除头尾路段一样的候选路线 Candidate_Routes = Common_Functions.Start_End( Candidate_Routes ) # 对于[wayid1,wayid2,wayid3] [wayid1,wayid4,wayid5,wayid3] 去除路段多的,如果包含路段数量一致 暂不处理 Candidate_Routes = Common_Functions.Sequential_subset( Candidate_Routes) # 最后去前缀 #print(len(Candidate_Routes)) delsub = [] for sub in Candidate_Routes: #判断行驶方向 secondgrid = Getway_startendnode_grid(sub[-1], flag=0) if secondgrid: curx = secondgrid[0] cury = secondgrid[1] if Endx > startx and Endy > starty: #路段way_id2 在way_id1 的右上部 if curx < startx and cury < starty: delsub.append(sub) #此路线方向向左下部走 删除此路线 if Endx > startx and Endy < starty: # 路段way_id2 在way_id1 的右下部 if curx < startx and cury > starty: delsub.append(sub) # 此路线方向向左上部走 删除此路线 if Endx < startx and Endy < starty: # 路段way_id2 在way_id1 的左下部 if curx > startx and cury > starty: delsub.append(sub) # 此路线方向向右上部走 删除此路线 if Endx < startx and Endy > starty: # 路段way_id2 在way_id1 的左上部 if curx > startx and cury < starty: delsub.append(sub) # 此路线方向向右上部走 删除此路线 else: delsub.append(sub) for sub in Common_Functions.Double_layer_list(delsub): Candidate_Routes.remove(sub) #print(len(Candidate_Routes)) if len(AllNextways) == 0: #所有的候选路线都没有下一步路可走 flag = 1 exitflag = 1 minnum = float("inf") if exitflag == 1: #证明是循环跳出不是因为找到路径跳出的 Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False Deleteways = [] #print(Candidate_Routes) for sub in Candidate_Routes: #由于以上为没有方向的判断,所以在此循环中要加入方向的判断 if way_id2 in sub: if len(sub) == 1: return sub elif len(sub) == 2 and JudgeTwoWay(sub[0], sub[1]): pass elif len(sub) >= 3 and JudgeLines(sub): pass else: Deleteways.append(sub) else: Deleteways.append(sub) if len(Deleteways) != 0: for delsub in Common_Functions.Double_layer_list(Deleteways): Candidate_Routes.remove(delsub) if len(Candidate_Routes) == 0: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False for sub in Candidate_Routes: if way_id2 in sub: if len(sub) < minnum: finalroute = sub minnum = len(sub) else: pass if len(finalroute) == 0: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False else: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 1) return finalroute
def waytoway(way_id1, way_id2, max_num=6): """ 实现从way_id1到way_id2的路线规划,当此函数完全用作简易导航,可设置max_num为无穷 设置 max_num 与Count 的原因为防止查找完全不通的两个路段而耗费过长的时间 有方向通行 问题:即使很近的两个路段(实际短距离不能通行),但是会花费较多的时间去判断是否能通行 47574526,403874395两个路段不能互通 但是只在北野场桥一部分就花费了5分钟去判断可行性 :param way_id1: :param way_id2: :return: 首先判断两个路段是否有交集,有交集则这两条路不需要经过其他路线的连接 """ if way_id1 == way_id2: return [way_id1, way_id2] finalroute = [] node_id = TwoWay_intersection(way_id1, way_id2) #两条路段交点 if node_id: if JudgeTwoWay(way_id1, way_id2): finalroute.extend([way_id1, way_id2]) Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 1) return finalroute else: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False else: #两条路段没有直接交点 Candidate_Routes = [[way_id1]] # 候选路线 flag = 0 count = 0 # 迭代多少次之后仍然没有找到可行路线,则认为不可走 exitflag = 0 #标记是否是通过找到满足条件的路线而退出的 grid1 = Getway_startendnode_grid(way_id1, flag=0) startx = grid1[0] starty = grid1[1] grid2 = Getway_startendnode_grid(way_id1, flag=0) Endx = grid2[0] Endy = grid2[1] startendwaydis = twoway_distance( way_id1, way_id2) + 0.01 # 加1000米弹性范围,0.0003为30米 while True: #print(Candidate_Routes) #print(count) temCandidate_Routes = [] # 存储当前这一轮新的候选路线 AllNextways = [] for subroute in Candidate_Routes: # subroute 表示正在处理的路线 processingway = subroute[-1] # 表示要处理的路段 nextway = NodirectionFindNextWay(processingway, subroute) # 下一步的可选路段 if len(nextway) == 0: # 当前路线下一步没有能走的路 pass else: AllNextways.extend(nextway) for next in nextway: temroute = copy.deepcopy(subroute) temroute.append(next) temCandidate_Routes.append(temroute) if len(AllNextways) == 0: #所有的候选路线都没有下一步路可走 flag = 1 exitflag = 1 count += 1 Candidate_Routes.clear() Candidate_Routes = temCandidate_Routes Candidate_Routes = Common_Functions.Double_layer_list( Candidate_Routes) Candidate_Routes = Common_Functions.Main_Auxiliary_road( Candidate_Routes) # 去除头尾路段一样的候选路线 Candidate_Routes = Common_Functions.Start_End( Candidate_Routes ) # 对于[wayid1,wayid2,wayid3] [wayid1,wayid4,wayid5,wayid3] 去除路段多的,如果包含路段数量一致 暂不处理 Candidate_Routes = Common_Functions.Sequential_subset( Candidate_Routes) # 最后去前缀 delsub = [] for sub in Candidate_Routes: if way_id2 in sub: flag = 1 break if len(Candidate_Routes) == 0: flag = 1 exitflag = 1 if count == max_num: flag = 1 exitflag = 1 if flag == 1: break if count > 4: #加入这个条件是为了防止找较近的不通路段时,由于方向问题,删除了方向错误的路线,导致查找时间过长 for sub in Candidate_Routes: if len(sub) >= 3: if JudgeLines(sub): # 判断当前路线是否能通 pass elif not JudgeLines( sub) and way_id2 in sub: # 目标路段在子路线中 但是不能走通 Common_Functions.SaveRoutesConn( "connects", way_id1, way_id2, 0) return False else: delsub.append(sub) continue elif len(sub) == 2: if JudgeTwoWay(sub[0], sub[1]): pass elif JudgeTwoWay( sub[0], sub[1] ) and way_id2 in sub: # 目标路段在子路线中 但是不能走通 Common_Functions.SaveRoutesConn( "connects", way_id1, way_id2, 0) return False else: delsub.append(sub) continue else: pass for sub in Candidate_Routes: # 判断行驶方向 secondgrid = Getway_startendnode_grid(sub[-1], flag=0) if secondgrid: curx = secondgrid[0] cury = secondgrid[1] if Endx > startx and Endy > starty: # 路段way_id2 在way_id1 的右上部 if abs(startx - curx) <= 5 or abs( starty - cury) <= 5: # 防止道路过近,出现偏差,方向加500米偏差 pass elif curx <= startx and cury <= starty: delsub.append(sub) # 此路线方向向左下部走 删除此路线 if Endx > startx and Endy < starty: # 路段way_id2 在way_id1 的右下部 if abs(startx - curx) <= 5 or abs( starty - cury) <= 5: # 防止道路过近,出现偏差,方向加500米偏差 pass elif curx < startx and cury > starty: delsub.append(sub) # 此路线方向向左上部走 删除此路线 if Endx < startx and Endy < starty: # 路段way_id2 在way_id1 的左下部 if abs(startx - curx) <= 5 or abs( starty - cury) <= 5: # 防止道路过近,出现偏差,方向加500米偏差 pass elif curx > startx and cury > starty: delsub.append(sub) # 此路线方向向右上部走 删除此路线 if Endx < startx and Endy > starty: # 路段way_id2 在way_id1 的左上部 if abs(startx - curx) <= 5 or abs( starty - cury) <= 5: # 防止道路过近,出现偏差,方向加500米偏差 pass elif curx > startx and cury < starty: delsub.append(sub) # 此路线方向向右上部走 删除此路线 else: pass temstaryendwaydis = twoway_distance(way_id1, sub[-1]) if temstaryendwaydis > startendwaydis: delsub.append(sub) for sub in Common_Functions.Double_layer_list(delsub): Candidate_Routes.remove(sub) if exitflag == 1: #证明是循环跳出不是因为找到路径跳出的 Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False Deleteways = [] for sub in Candidate_Routes: # 由于以上前四段没有方向的判断,所以在此循环中要加入方向的判断 if way_id2 in sub: if len(sub) == 1: return sub elif len(sub) == 2 and JudgeTwoWay(sub[0], sub[1]): pass elif len(sub) >= 3 and JudgeLines(sub): pass else: Deleteways.append(sub) else: Deleteways.append(sub) if len(Deleteways) != 0: for delsub in Common_Functions.Double_layer_list(Deleteways): Candidate_Routes.remove(delsub) if len(Candidate_Routes) == 0: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False minnum = float("inf") for sub in Candidate_Routes: if way_id2 in sub: if len(sub) < minnum: finalroute = sub minnum = len(sub) else: pass if len(finalroute) == 0: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 0) return False else: Common_Functions.SaveRoutesConn("connects", way_id1, way_id2, 1) return finalroute
def FindPointCandidateWay(csvfilepath, candidatewaypath, candidatewayname): """ 找出坐标点的候选路段,此部分已经通过角度(大于90)、距离(大于30米)筛除一部分候选路段 :param csvfilepath: csv文件路径 例:H:\TrunksArea\\334e4763-f125-425f-ae42-8028245764fe.csv" :param candidatewaypath: 轨迹点候选路段保存路径 :param candidatewayname: 候选路段保存的文件名 :return: """ # 读时间 经纬度 网格编号 #df = pd.read_csv("H:\GPS_Data\Road_Network\BYQBridge\TrunksArea\\334e4763-f125-425f-ae42-8028245764fe.csv",header=None, usecols=[1, 2, 3, 4, 5]) df = pd.read_csv(csvfilepath, header=None, usecols=[1, 2, 3, 4, 5]) points_num = df.shape[0] # 坐标数量 pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) # print(df.iloc[:,1:4]) drop_list = [] # 要删除的索引列表 for row in range(1, points_num): if row == points_num: break points_dis = Common_Functions.haversine(df.iloc[row, 1], df.iloc[row, 2], df.iloc[row - 1, 1], df.iloc[row - 1, 2]) # 相邻坐标点之间的距离 if points_dis < 0.01: # 距离小于10米 drop_list.append(row) # print(drop_list) newdf = df.drop(drop_list) # 删除相邻点在10米之内的点 # print(newdf.iloc[:,1:3]) newdf = newdf.reset_index(drop=True) #file = open("H:\GPS_Data\Road_Network\BYQBridge\CandidateWay\\NewStrategy\\334e4763-f125-425f-ae42-8028245764fe.txt", 'a') txtname = candidatewayname + ".txt" file = open(os.path.join(candidatewaypath, txtname), 'a') print("本车辆共查找坐标点数为:{}".format(newdf.shape[0])) for row in range(newdf.shape[0]): if row == 0: #print("处理起始坐标点{}".format([newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4]])) dic = Common_Functions.Find_Candidate_Route([ newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4] ], [ newdf.iloc[row + 1, 1], newdf.iloc[row + 1, 2], newdf.iloc[row + 1, 3], newdf.iloc[row + 1, 4] ], flag=1) if dic: # 有候选路段才保存 file.write("PointID-" + str(row + 1) + "CandidateWay>>>" + str(dic) + "\n") elif row == newdf.shape[0] - 1: #print("处理终点坐标点{}".format([df.iloc[row - 1, 2], df.iloc[row - 1, 3], df.iloc[row, 2], df.iloc[row, 3]])) dic = Common_Functions.Find_Candidate_Route([ newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2], newdf.iloc[row - 1, 3], newdf.iloc[row - 1, 4] ], [ newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4] ], flag=2) if dic: file.write("PointID-" + str(row + 1) + "CandidateWay>>>" + str(dic) + "\n") else: dis1 = Common_Functions.haversine(newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2]) dis2 = Common_Functions.haversine(newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row + 1, 1], newdf.iloc[row + 1, 2]) # 找相邻最近的点做为轨迹方向 if dis2 > dis1: #print("处理终点坐标点{}".format([newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2], newdf.iloc[row - 1, 3], newdf.iloc[row - 1, 4]])) dic = Common_Functions.Find_Candidate_Route([ newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2], newdf.iloc[row - 1, 3], newdf.iloc[row - 1, 4] ], [ newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4] ], flag=2) else: #print("处理起始坐标点{}".format([newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4]])) dic = Common_Functions.Find_Candidate_Route([ newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4] ], [ newdf.iloc[row + 1, 1], newdf.iloc[row + 1, 2], newdf.iloc[row + 1, 3], newdf.iloc[row + 1, 4] ], flag=1) if dic: file.write("PointID-" + str(row + 1) + "CandidateWay>>>" + str(dic) + "\n") file.close()
def BatchSelectFinalRoute(Candidatewaypath, finalroutepath): candidatetxts = Common_Functions.findtxtpath(Candidatewaypath) for subway in candidatetxts: print(subway) SelectFinalRoute(subway, finalroutepath)
def SelectFinalRoute(candidatewaypath, savefinalroutespath): """ 根据坐标点的候选路段选出路网的匹配路线 保存格式为:车辆名:路线(如果不确定,可能为多条),车辆名为txt文件名 :param candidatewaypath: 坐标点候选路段的txt文件路径,如H:\\CandidateWay\\NewStrategy\\334e4763-f125-425f-ae42-8028245764fe.txt :param savefinalroutespath: 最终路线保存路径 :return: """ #file = open("H:\GPS_Data\Road_Network\BYQBridge\FinalRoutes\\334e4763-f125-425f-ae42-8028245764fe.txt", 'a') (tempath, tempfilename) = os.path.split( candidatewaypath) # tempfilename为txt文件名(包含后缀) (trunkname, extension) = os.path.splitext( tempfilename) # filename 为传入的csv文件名 extension为后缀 savetxtfilename = trunkname + '.txt' file = open(os.path.join(savefinalroutespath, savetxtfilename), 'a') with open(candidatewaypath) as candidatewayfile: filelines = candidatewayfile.readlines() linesnum = len(filelines) finalline = [] #存储最终路线,可能为多条,随着坐标点的迭代,会变化,直到处理完最有一个坐标点 for key in eval(filelines[0].strip('\n').split(">>>")[-1]).keys(): finalline.append([key]) #print(finalline) # 遍历每个坐标点的候选路段 print("需要处理坐标数为:{}".format(linesnum)) for lineindex in range(1, linesnum): templine = [] #存储临时路线 # 遍历到最后一行 print(len(finalline)) print(finalline) print("处理坐标{}:{}".format( lineindex, eval(filelines[lineindex].strip('\n').split(">>>")[-1]))) #print("处理路段{}".format(eval(filelines[lineindex].strip('\n').split(">>>")[-1]))) for subline in finalline: for key in eval(filelines[lineindex].strip('\n').split(">>>") [-1]).keys(): temsubline = [] #此代码块只加入key,不加入完整路线 print("路段:{}匹配key:{}".format(subline[-1], key)) # 只需要查看subline的最后一个路段与路段key是否连通即可,因为subline的连通性是通过测试的 connectroute = Common_Functions.InquireConn( subline[-1], key, "connects") #先查表 #connectroute = -1 if connectroute != 0 and connectroute != 1: #表中没有记录 再用简易导航 connectroute = MapNavigation.waytoway( subline[-1], key) # 为列表 if connectroute: temsubline = copy.deepcopy(subline) temsubline.append(key) # 只加入轨迹点所属路段,而不加入这两个路段走通的路线 templine.append(temsubline) else: # 此路线不连通,舍弃当前路段key pass """ #此代码块是加入完整路线 #connectroute = Common_Functions.InquireConn(subline[-1], key,"connects") #先查表 #connectroute = -1 connectroute = MapNavigation.Nodirectionwaytoway(subline[-1], key) # 为列表 if subline[-1] == key: temsubline = copy.deepcopy(subline) templine.append(temsubline) #elif connectroute !=0 and connectroute!= 1: #表中没有记录 再用简易导航 #connectroute = MapNavigation.Nodirectionwaytoway(subline[-1], key) # 为列表 elif connectroute: #路段可连通 temsubline = copy.deepcopy(subline) temsubline.extend(connectroute[1:]) #将走通的路线加入到子路线,扩展当前路线 templine.append(temsubline) else:pass """ # print(temsubline) # print(templine) finalline.clear() #print(templine) finalline = Common_Functions.DoubleDel(templine) #去相邻重复 再去重 finalline = Common_Functions.Main_Auxiliary_road( finalline) #去除头尾路段一样的候选路线,路线只有一个路段 不会处理 #print(finalline) finalline = Common_Functions.Start_End( finalline ) # 对于[wayid1,wayid2,wayid3] [wayid1,wayid4,wayid5,wayid3] 去除路段多的,如果包含路段数量一致 暂不处理 finalline = Common_Functions.Sequential_subset( finalline) # 最后去路线(至少两个及以上的其他路线是其前缀) #print(finalline) # finalline = Common_Functions.Double_layer_list(templine) #去重 # finalline = Common_Functions.Sequential_subset(finalline) #去除前缀(两个及以上) #file.write(str(finalline) + "\n") #file.flush() print("共选出{}条路".format(len(finalline))) for sub in finalline: file.write(str(sub) + "\n") file.flush() print(finalline) file.close()