如何在多個 csv 文件中循環使用腳本繼上一篇文章之後,我有一個腳本可以堆疊和格式化一個笨重的 csv 文件。
https://community.jmp.com/t5/Discussions/Help-doing-a-table-transform-and-converting-a-timestamp/td-p/303091
我尋求幫助的下一個問題是如何才能在文件夾中的一組 csv 上重複此操作,將文件名捕獲為其中一列,理想情況下也將它們堆疊起來。又名步驟 1、步驟 2、步驟 3、步驟 4 文件。
堆疊單個 csv 可創建 400k 行輸出。 如果我合併 4 個、8 個或 10 個這些 csv,是否會有崩潰的風險?
也許額外的是然後再次循環瀏覽父文件夾中的下一個文件夾,創建一個新的組合步驟文件?
names default to here(1);
//Prompt to select and open an MS Excel file
dt = Open( Pick File( "Select Excel File", "C:\Users\folder\", {"Excel Files|csv;xlsx;xls", "All Files|*"}, 1, 0 ), Invisible );
// Get the column names from the data table
colNames = dt << get column names;
// Remove the 1st col from the list
remove from(colNames,1,1);
// Stack the data
dtStack = dt << Stack(
columns(
colNames
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Output Table( "Stacked_Data" )
);
//label the 1st column to Lamda
:Column 1 << Set Name("Lamda");
:Lamda << Modeling Type(nominal);
// Create a new JMP DateTime column
dtStack << New Column( "Timestamp",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 26, 3 ),
Input Format( "d/m/y h:m:s", 0 ),
Formula( Informat( Word( 1, :Label, "Z" ), "d/m/y h:m:s" ) )
);
// Remove formula to convert column to static values
dtStack:Timestamp << delete formula;
// Create the Time variable column
dtstack << New Column( "Time",
Numeric,
"Continuous",
Format( "hr:m:s", 17, 3 ),
Input Format( "hr:m:s", 0 ),
Formula( Time Of Day( :Timestamp ) )
);
//Create Absolute Time column, uses if>then error checking for Lamda group changing
dtstack << New Column( "AbsTime",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 1 ),
Formula(
If( Row() == 1 | :Lamda != Lag( :Lamda ),
0,
Lag( :AbsTime ) + (:Timestamp - Lag( :Timestamp ))
)
),
);
// Remove formula to convert column to static values
dtStack:AbsTime << delete formula;
// Get rid of no longer needed "Label" column
dtStack << delete columns( "Label" );
數據混合和清理回复:如何在多個 csv 文件中循環腳本看起來代碼想要使用 filelist 而不是 filename 作為文件名列表。 接近尾聲的那個。
回复:如何在多個 csv 文件中循環腳本我看了這個,但似乎不符合我的需求。 它只會打開各個 jmp dt。 列# 是動態的,基於每個進程文件的時間範圍。
回复:如何在多個 csv 文件中循環腳本驚人的。 它似乎工作正常,但在第一個文件之後出現錯誤
“名稱未解析:訪問或評估 'filename' 時的 filenam}1},filename/*###*/”
有什麼想法嗎?
我還調整了腳本以執行選擇文件夾彈出窗口,我認為這不是問題。
// Get a list of files from the directory
dir = Pick Directory( "Select a directory", "C:\Users\folder\");
fileList = Files In Directory( dir );
// Create a skeleton data table to end up being the combined data table
dtFinal = New Table("Final");
// Loop across all of the files found in the directory and read them in
For(i = 1, i <= N Items( fileList ), i++,
dt = open( dir || fileList[i] );回复:如何在多個 csv 文件中循環腳本另請查看多個文件導入。 它加載 CSV 文件,如果它們具有相同的列,則將它們連接起來,並且可以選擇添加文件名列。
回复:如何在多個 csv 文件中循環腳本基本上,您所要做的就是
- 使用“Files In Directory()”獲取要處理的文件列表
- 設置一個數據表用於最終數據表( dtFinal )
- 使用 For() 循環遍歷創建 deStack 表的目錄中找到的所有文件
- 將源列添加到 dtStack
- 將 dtStack 連接到 dtFinal 表
- 關閉dtStack和dt數據表
以下是使用上述項目修改的腳本。該代碼未經測試,您必須添加目錄等才能從中讀取文件。它應該為您獲得最終產品提供一個真正良好的起點
names default to here(1);
//Prompt to select and open an MS Excel file
//dt = Open( Pick File( "Select Excel File", "C:\Users\folder\", {"Excel Files|csv;xlsx;xls", "All Files|*"}, 1, 0 ), Invisible );
// Get a list of files from the directory
fileList = Files In Directory( <PATH to="" your="" directory=""> || fileList[i] );
// Get the column names from the data table
colNames = dt << get column names;
// Remove the 1st col from the list
remove from(colNames,1,1);
// Stack the data
dtStack = dt << Stack(
columns(
colNames
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Output Table( "Stacked_Data" )
);
//label the 1st column to Lamda
:Column 1 << Set Name("Lamda");
:Lamda << Modeling Type(nominal);
// Create a new JMP DateTime column
dtStack << New Column( "Timestamp",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 26, 3 ),
Input Format( "d/m/y h:m:s", 0 ),
Formula( Informat( Word( 1, :Label, "Z" ), "d/m/y h:m:s" ) )
);
// Remove formula to convert column to static values
dtStack:Timestamp << delete formula;
// Create the Time variable column
dtstack << New Column( "Time",
Numeric,
"Continuous",
Format( "hr:m:s", 17, 3 ),
Input Format( "hr:m:s", 0 ),
Formula( Time Of Day( :Timestamp ) )
);
//Create Absolute Time column, uses if>then error checking for Lamda group changing
dtstack << New Column( "AbsTime",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 1 ),
Formula(
If( Row() == 1 | :Lamda != Lag( :Lamda ),
0,
Lag( :AbsTime ) + (:Timestamp - Lag( :Timestamp ))
)
),
);
// Remove formula to convert column to static values
dtStack:AbsTime << delete formula;
// Get rid of no longer needed "Label" column
dtStack << delete columns( "Label" );
// Add filename as a column to the data table
dtStack << new column( "Source", character, set each value( filename[i] ) );
// Concatenate to final table
dtFinal << concatenate( dtStack, append to first table(1) );
// Clean up
close( dtStack, nosave );
close( dt, nosave );
// End For() Loop
);</PATH>