從具有相同結構的資料夾中提取並連接 csv 文件<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />你好,
例如,我在不同的資料夾中有一大堆具有共同名稱的 csv 文件,並且 csv 文件具有相同的結構
<第一個資料夾>dfgdwgssd
<具有通用名稱的第二個資料夾> commonname
xxx-通用名稱.csv
我正在考慮使用腳本來提取和連接所有 csv 檔案。 我找到了一種使用 bash 命令的簡單方法(“for”循環與“find”一起),有人可以幫我嗎? 太感謝了!
回覆:從具有相同結構的資料夾中提取並連接 csv 文件再次感謝你,菲爾!
我使用的是 Mac,剛剛在 Google 上搜索,發現有關從 JMP 運行 shell 的信息很少,您有一些示例嗎?
回覆:從具有相同結構的資料夾中提取並連接 csv 文件當然是我的榮幸! 我試圖指出,您仍然可以使用 JMP 檢索整個文件列表,然後使用 REGEX 搜尋包含 的檔案名稱。 事實上,我可以想像有 1300 多個資料夾可能需要一些時間。
關於速度的幾點建議:
- 列印到日誌確實會減慢速度。
- 另外,我會先打開所有文件,然後再連接並關閉每個文件。
*如果您使用的是 Mac,則可以從 JMP 啟動 shell 腳本。 在 Windows 上,除非您使用 Cygwin 或類似的東西,否則 Powershell 是編寫可從 JMP 呼叫的實用程式流程的不錯選擇。
菲爾
回覆:從具有相同結構的資料夾中提取並連接 csv 文件嗨菲爾,
非常感謝您的提示! 不過我沒有運行你的腳本。
我使用少量資料夾進行嘗試,後來意識到使用大量資料夾(例如 >1,300)需要很長時間,因為 JMP 將轉到第二個目錄並提取 *.csv 檔案等,非常非常時間 -雖然消耗。
為了提高效率,我想到用bash將所有csv檔案移到一個資料夾中,然後將它們連接起來。 使用 bash 對超過 1,300 個資料夾執行此操作(提取和連接 csv 檔案)的快速檢查花了我 3 秒,而連接的 csv 檔案為 13MB,3 秒! 我需要研究在 JSL 中執行 bash 命令的可能性。
順便提一句,
dt = Open( dirpath || TheFileList,invisible );
處理時不會彈出資料表
再次感謝你!
回覆:從具有相同結構的資料夾中提取並連接 csv 文件你好傑威,
除了吉姆的建議之外,您還可以嘗試以下通用方法來選擇所需的文件。 請參閱下面的範例:
allFiles = FilesInDirectory("$Documents", recursive);
selectedFiles = {};
commonName = "BrownP";
For( i = 1, i<=NItems(allFiles), i++,
If( !IsMissing( Regex(allFiles, EvalInsert(".*<COMMONNAME>.*\.csv","<",">" )) ),
InsertInto(selectedFiles, allFiles);
)
);</COMMONNAME>
1. 檢索「我的文件」資料夾及其子資料夾中的所有檔案名稱。 (替換為您的頂級資料夾路徑)。
2. 建立一個空列表來儲存所需的檔案名稱。
3. 定義commonName 字串。
4. 循環遍歷所有檔案名,僅選擇與模式相符的檔案名稱並儲存在選定的文件列表。 在這個例子中我正在檢索文件名中包含“BrownP”的所有 CSV 文件。 (替換為您自己的 commonName 字串)。
因此,完整的實作(使用上一篇文章中的 Jim 的程式碼):
Names Default To Here( 1 );
// Setup an empty table to start to concatenate other tables to
dtbase = New Table( "Base Table" );
// Have the user select the starting folder
dirpath = Pick Directory( "Pick the Top Level Directory" );
// Get all of the files in all of the folders that are under the picked folder
TheFileList = Files In Directory( dirpath, "recursive" );
// Define common string to search for
commonName = "file";
// Go through all of the files and read in append those that meet your
// requirement. You will need to amend the IF statement to meet your criteria
For( i = 1, i <= N Items( TheFileList ), i++,
If( !Is Missing( Regex( TheFileList, Eval Insert( ".*<COMMONNAME>.*\.csv", "<", ">" ) ) ),
dt = Open( dirpath || TheFileList );
dtbase = dtbase << concatenate( dt, append to first table( 1 ) );
Close( dt, nosave );
)
);</COMMONNAME>
希望這可以幫助!
最好的,
菲爾
回覆:從具有相同結構的資料夾中提取並連接 csv 文件
Names Default To Here( 1 );
// Setup an empty table to start to concatenate other tables to
dtbase = New Table( "Base Table" );
// Have the user select the starting folder
dirpath = Pick Directory( "Pick the Top Level Directory" );
// Get all of the files in all of the folders that are under the picked folder
TheFileList = Files In Directory( dirpath, "recursive" );
// Go through all of the files and read in append those that meet your
// requirement. You will need to amend the IF statement to meet your criteria
For( i = 1, i <= N Items( TheFileList ), i++,
If( Uppercase( Word( -1, TheFileList[i], "." ) ) == "CSV" & Is File( dirpath || TheFileList[i] ) == 1,
If( Contains ( TheFileList[i], "<2nd folder common name"),
If( Contains (TheFileList[i],"<COMMON name="" of="" the="" csvfile="">"),
print(TheFileList[i]);
dt = Open( dirpath || TheFileList[i] );
dtbase = dtbase << concatenate( dt, append to first table( 1 ) );
Close( dt, nosave );
);
);
);
);</COMMON>
再增加兩行才能使其正常工作,但需要調整對齊/縮排。
謝謝你!
回覆:從具有相同結構的資料夾中提取並連接 csv 文件是的,我確實知道第二層和 csv 檔案的「通用名稱」是什麼。 謝謝你!
回覆:從具有相同結構的資料夾中提取並連接 csv 文件您提前知道「俗名」是什麼嗎?或者你需要讓腳本來解決這個問題嗎?在這種情況下,資料夾中是否會出現多個可能的通用名稱?
回覆:從具有相同結構的資料夾中提取並連接 csv 文件嗨吉姆,
我讓它工作了,但它列出了所有 CSV 文件,我試圖找到整理或指定第二層的方法。能提供更多提示嗎?
謝謝你!
回覆:從具有相同結構的資料夾中提取並連接 csv 文件這是一個應該可以為您提供良好開端的腳本:
Names Default To Here( 1 );
// Setup an empty table to start to concatenate other tables to
dtbase = New Table( "Base Table" );
// Have the user select the starting folder
dirpath = Pick Directory( "Pick the Top Level Directory" );
// Get all of the files in all of the folders that are under the picked folder
TheFileList = Files In Directory( dirpath, "recursive" );
// Go through all of the files and read in append those that meet your
// requirement. You will need to amend the IF statement to meet your criteria
For( i = 1, i <= N Items( TheFileList ), i++,
If( Uppercase( Word( -1, TheFileList[i], "." ) ) == "CSV" & Is File( dirpath || TheFileList[i] ) == 1,
dt = Open( dirpath || TheFileList[i] );
dtbase = dtbase << concatenate( dt, append to first table( 1 ) );
Close( dt, nosave );
);
);