我有大約 20000 個檔案,我想根據閾值計算每個檔案的峰值(我有 10 個閾值),然后將其保存在 csv 檔案中。我很困惑如何根據 csv 檔案中的閾值保存檔案的值。
for threshold in np.arange(1,10,1):
threshold_p=calculate_th(n,m, threshold)
for root, dirs, files in os.walk(dir_path, topdown=False):
for name in files:
allfiles.append(os.path.join(root, name))
for filename in tqdm.tqdm(allfiles, desc= "files progress"):
output_g = np.load(filename)
filtered=np.sum(output_g > threshold_p)
result= [filename,filtered, threshold_p, threshold]
我想將“結果”值保存為 4 列的 csv 檔案,但我無法將它們保存為 csv 檔案而不重寫它們。
uj5u.com熱心網友回復:
你沒有展示你是如何撰寫它的,但可以有不同的方法來做到這一點。
第一的。有一個標準規則:如果您使用for-loop 則用于list保留所有結果。
通過這種方式,您可以在回圈前創建空串列,將結果追加到回圈內的該串列中,并在回圈后寫入所有結果。
為了使所有版本都相似,我不會使用with open(..) as fh- 但您可以嘗試使用它。
# --- before loop ---
all_results = [] # <-- list for all results
# --- loop ---
for threshold in np.arange(1,10):
threshold_p = calculate_th(n, m, threshold)
for root, dirs, files in os.walk(dir_path, topdown=False):
for name in tqdm.tqdm(files, desc="files progress"):
filename = os.path.join(root, name)
output_g = np.load(filename)
filtered = np.sum(output_g > threshold_p)
result = [filename, filtered, threshold_p, threshold]
all_results.append( result ) # <-- keep result
# --- after loop ---
fh = open('output.csv', 'w')
cvs_writer = cvs.writer(fh)
# write one row with headers (using `writerow` without `s` at the end)
cvs_writer.writerow(["filename", "filtered", "threshold_p", "threshold"] cvs_writer.writerow(["filename", "filtered", "threshold_p", "threshold"]
# write many rows with results (using `writerows` with `s` at the end)
cvs_writer.writerows(all_results)
fh.close()
第二。您應該在回圈前打開檔案,在回圈內寫入行,并在回圈后關閉檔案。
# --- before loop ---
fh = open('output.csv', 'w')
cvs_writer = cvs.writer(fh)
# write one row with headers (using `writerow` without `s` at the end)
cvs_writer.writerow(["filename", "filtered", "threshold_p", "threshold"] cvs_writer.writerow(["filename", "filtered", "threshold_p", "threshold"]
# --- loop ---
for threshold in np.arange(1,10):
threshold_p = calculate_th(n, m, threshold)
for root, dirs, files in os.walk(dir_path, topdown=False):
for name in tqdm.tqdm(files, desc="files progress"):
filename = os.path.join(root, name)
output_g = np.load(filename)
filtered = np.sum(output_g > threshold_p)
result = [filename, filtered, threshold_p, threshold]
# write row row with result (using `writerow` without `s` at the end)
cvs_writer.writerow(result)
# --- after loop ---
fh.close()
第三。在回圈之前只創建帶有標題的檔案 - 并關閉它。內部回圈打開檔案append mode以在檔案末尾添加新行。
如果代碼崩潰,此方法將保留所有結果。
# --- before loop ---
fh = open('output.csv', 'w')
cvs_writer = cvs.writer(fh)
# write one row with headers (using `writerow` without `s` at the end)
cvs_writer.writerow(["filename", "filtered", "threshold_p", "threshold"] cvs_writer.writerow(["filename", "filtered", "threshold_p", "threshold"]
fh.close()
# --- loop ---
for threshold in np.arange(1,10):
threshold_p = calculate_th(n, m, threshold)
for root, dirs, files in os.walk(dir_path, topdown=False):
for name in tqdm.tqdm(files, desc="files progress"):
filename = os.path.join(root, name)
output_g = np.load(filename)
filtered = np.sum(output_g > threshold_p)
result = [filename, filtered, threshold_p, threshold]
fh = open('output.csv', 'a') # `a` for `append mode`
cvs_writer = cvs.writer(fh)
# write row row with result (using `writerow` without `s` at the end)
cvs_writer.writerow(result)
fh.close()
# --- after loop ---
# nothing
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/397996.html
上一篇:在awk腳本中讀取CSV檔案
