如何通過多個csv文件循環腳本在前一篇文章之後,我得到了一個腳本,該腳本可以堆疊並格式化笨拙的csv文件。
https://community.jmp.com/t5/Discussions/Help-doing-a-table-transform-and-converting-a-timestamp/td-p/303091
我下一個需要幫助的問題是,如何才能在文件夾中的一組csv上重複該操作,將文件名捕獲為列之一,最好也將它們堆疊起來。又名step1,step2,step3,step4文件。
堆疊單個csv可創建400k行輸出。 如果我將這些csv的4 ot 8或10合併,是否會有崩潰的風險?
也許額外的需要是然後再次循環遍歷父文件夾中的下一個文件夾,從而創建新的組合步驟文件?
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文件循環腳本基本上,您要做的就是
- 使用“ Files In Directory()獲取要處理的文件列表
- 設置要用於最終數據表的數據表(dtFinal)
- 使用For()循環遍歷創建deStack表的目錄中找到的所有文件
- 將Source列添加到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>回复:如何通過多個csv文件循環腳本還要檢查“多文件導入”。 它會加載CSV文件,如果它們具有相同的列,則將它們串聯起來,並且可以選擇添加文件名列。
回复:如何通過多個csv文件循環腳本太棒了 它似乎正在運行,但是在第一個文件後出現錯誤
“名稱未解析:filenam} 1}正在訪問或評估'filename',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] );