2014-01-21 69 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