我對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