我对python很陌生。我正在水文领域工作,我想学习python以帮助我处理水文数据。如何提高python脚本的速度
此刻我写了一个脚本来从大数据集中提取信息的位。我有三个CSV文件:
Complete_borelist.csv
Borelist_not_interested.csv
Elevation_info.csv
我想建立一个档案,有凡在complete_borelist.csv但不是孔在borelist_not_interested.csv中。我也想从complete_borelist.csv和Elevation_info.csv中获取满足第一条标准的信息。
我的脚本如下:
not_interested_list =[]
outfile1 = open('output.csv','w')
outfile1.write('Station_ID,Name,Easting,Northing,Location_name,Elevation')
outfile1.write('\n')
with open ('Borelist_not_interested.csv','r') as f1:
for line in f1:
if not line.startswith('Station'): #ignore header
line = line.rstrip()
words = line.split(',')
station = words[0]
not_interested_list.append(station)
with open('Complete_borelist.csv','r') as f2:
next(f2) #ignore header
for line in f2:
line= line.rstrip()
words = line.split(',')
station = words[0]
if not station in not_interested_list:
loc_name = words[1]
easting = words[4]
northing = words[5]
outfile1.write(station+','+easting+','+northing+','+loc_name+',')
with open ('Elevation_info.csv','r') as f3:
next(f3) #ignore header
for line in f3:
line = line.rstrip()
data = line.split(',')
bore_id = data[0]
if bore_id == station:
elevation = data[4]
outfile1.write(elevation)
outfile1.write ('\n')
outfile1.close()
我有两个问题与脚本:
首先是Elevation_info.csv没有相关信息的Complete_borelist.csv所有的孔。当我的循环到达无法找到Elevation记录的站点时,脚本不会写入“空”,而是继续在同一行中写入下一个站的信息。任何人都可以帮我解决这个问题吗?
第二个是我的完整钻孔列表是大约200000行,我的脚本非常缓慢地贯穿它们。任何人都可以有任何建议让它跑得更快吗?
非常感谢和抱歉,如果我的问题太长。
非常感谢。它现在在几秒钟内工作:) – mikayla