2014-01-21 45 views
0

我对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行,我的脚本非常缓慢地贯穿它们。任何人都可以有任何建议让它跑得更快吗?

非常感谢和抱歉,如果我的问题太长。

回答

0

性能方面,这有几个问题。第一个是打开并重新读取整个文件的每一行的海拔信息。在打开完整文件之前,将海拔信息读取到在bore_id上​​键入的字典。然后,您可以非常快速地测试字典,以查看工作站是否在其中,而不是重新阅读。

第二个性能问题是,一旦找到匹配项,您不会停止在bore_id列表中搜索。字典的想法也解决了这个问题,但是一旦你有了一场比赛,休息一下会有所帮助。

对于null打印问题,如果bore_id不在字典中,您只需要outfile1.write(“\ n”)。字典测试中的else语句就是这样做的。在当前的代码中,另一个关闭for循环会做到这一点。甚至改变最后写入的缩进(“\ n”)。

+0

非常感谢。它现在在几秒钟内工作:) – mikayla