如何在多个 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>