使用脚本对 csv 文件中的数据进行后处理我有一个包含多个 csv 文件的文件夹,这些文件具有类似的数据结构,我需要处理其数据并将结果存储在新文件中。
每个csv文件有2列,我需要按如下方式处理第二列中的数据:
- 将第二列命名为项目
- 对所有行执行以下操作,使用以下公式创建新列 x:36 + -19.5 * :items+ 49.99 *:项目*:项目*:项目
- 对创建列 y 的 x 列的所有行执行以下操作:
平均值( :x[索引( Row() - 4, Row() )] ) - 求 y 列的平均值和最大值
- 将平均值和最大值存储到文件中,并对其他文件重复相同的过程Pfa 示例 csv 文件。
我正在尝试如下所示的内容,但由于只是初学者,所以请您帮忙
// Read all the file names
files = Files In Directory( "C:\Users\MyUserName\Documents\My Data\" );
X = N Items( files );
Y = "C:\Users\MyUserName\Documents\My Data\";
//stores the mean and max value of each file
dt = New Table( "My data file");
For( i = 1, i < X, i++,
//open jmp files for each csv files.
dt1 = Open(
Y || files[i],
//Code to add column name and the find mean and max
);
dt1 << Set Name( files[i] );
//I concatenated all the files in one jmp file and close it each time.
dt << Concatenate( Data Table( dt1 ), "Append to first table" );
Close( dt1 );
);
消费者和市场研究内容组织数据存取数据混合和清理质量和工艺工程分享和交流结果回复:使用脚本对 csv 文件中的数据进行后处理这为解决问题奠定了良好的基础回复:使用脚本对 csv 文件中的数据进行后处理感谢您的优化和解决错误,非常有帮助!回复:使用脚本对 csv 文件中的数据进行后处理你是如此接近哦......
您似乎正准备将每个 csv 读入后所需的统计数据放入列表中。您执行此操作的语法不正确,在我看来,您不必将值放入列表中,而是将它们直接放入汇总表中。另外,在计算列的统计数据时,需要使用统计数据的列形式(即 Col Max() )。最后,如果完成 csv 文件的处理后,不需要它继续保留,您可以简单地将其关闭。我添加了一行可以做到这一点,但我将其注释掉了。如果你想让它生效,只需删除//即可。
Names Default To Here( 1 );
// Use a function to open a .CSV file: Given the path to such a file, returns a reference to the resulting table
openMyCSV = Function( {path2File},
{Default Local},
dt = Open(
path2File,
columns(
New Column( "column 1", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "items", Numeric, "Continuous", Format( "Best", 12 ) )
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Comma, CSV( 0 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
)
);
// Start your summary table
dt = New Table( "My summary file" );
dt << New Column( "MovAvgMean", Numeric );
dt << New Column( "MovAvgMax", Numeric );
// Put all the .csv files to process into a list
dir = Pick Directory(
"Directory containing your csv files",
"C:\Users\shilpajo\Documents\PV\KT\LIMC\iccMax\DIADEM\JMP\files\"
);
files = Files In Directory( dir );
For( f = N Items( files ), f >= 1, f--,
If( !Ends With( Uppercase( files[f] ), ".CSV" ),
Remove From( files, f )
)
);
// Process each file - Incomplete!
For( f = 1, f <= N Items( files ), f++,
thisTable = openMyCSV( dir || files[f] );
thisTable << New Column( "x",
Numeric,
Continuous,
Formula( 36 + -19.5 * :items + 49.99 * :items * :items * :items )
);
thisTable << New Column( "av", Numeric, Continuous, Formula( Mean( :x[Index( Row() - 9, Row() )] ) ) );
// This will insure that all formula processing is complet before the next statement is run
thisTable << run formulas;
// Add the summary statistics to the summary table
dt << Add Rows( 1 );
dt:MovAvgMean[N Rows( dt )] = Col Mean( thisTable:av );
dt:MovAvgMax[N Rows( dt )] = Col Max( thisTable:av );
// Close( thisTable, nosave );
);
回复:使用脚本对 csv 文件中的数据进行后处理这段代码对我的入门非常有帮助。下面的代码完美运行。 我所说的存储是指我需要创建一个汇总表,其中包含每个 csv 文件的 av 列的平均值和最大值,我该怎么做?
我尝试使用: dt << Add Rows( {MovAvgMean =平均值(:av) , MovAvgMax = Max(:av)} ); // 单个列表
但它不起作用,因为我收到错误
名称未解析:访问或评估 'av' 时存在 av{1} ,:av/*###*/
NamesDefaultToHere(1);
// Use a function to open a .CSV file: Given the path to such a file, returns a reference to the resulting table
openMyCSV =
function({path2File}, {Default Local},
dt = Open(
path2File,
columns(
New Column( "column 1", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "items", Numeric, "Continuous", Format( "Best", 12 ) )
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Comma, CSV( 0 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
)
);
dt = New Table( "My data file");
dt << New Column( "MovAvgMean", Numeric );
dt << New Column( "MovAvgMax", Numeric );
// Put all the .csv files to process into a list
dir = pickDirectory("Directory containing your csv files", "$DESKTOP");
files = filesInDirectory(dir);
for(f = NItems(files), f>=1, f--, if(!endsWith(uppercase(files[f]), ".CSV"), removeFrom(files, f)));
// Process each file - Incomplete!
for(f = 1, f <= NItems(files), f++,
thisTable = openMyCSV(dir||files[f]);
thisTable << newColumn("x", Numeric, Continuous, Formula(36 + -19.5 * :items + 49.99 *:items* :items* :items));
thisTable << newColumn("av", Numeric, Continuous, Formula(Mean( :x[Index( Row() - 9, Row() )] )));
dt << Add Rows( {MovAvgMean =Mean(:av), MovAvgMax = Max(:av)} ); // single list
);
// Make the summary table
回复:使用脚本对 csv 文件中的数据进行后处理如果您刚刚开始,您应该分阶段进行,了解每个部分的工作原理。 使用“帮助 > 脚本索引”和“帮助 > 书籍 > 脚本指南”来查找内容。
下面的代码应该可以帮助您开始,并且您也许可以完成剩下的事情。 您需要更加明确“存储价值”的含义。 。 。到文件中'。
NamesDefaultToHere(1);
// Use a function to open a .CSV file: Given the path to such a file, returns a reference to the resulting table
openMyCSV =
function({path2File}, {Default Local},
dt = Open(
path2File,
columns(
New Column( "column 1", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "items", Numeric, "Continuous", Format( "Best", 12 ) )
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Comma, CSV( 0 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
)
);
// Put all the .csv files to process into a list
dir = pickDirectory("Directory containing your csv files", "$DESKTOP");
files = filesInDirectory(dir);
for(f = NItems(files), f>=1, f--, if(!endsWith(uppercase(files[f]), ".CSV"), removeFrom(files, f)));
// Process each file - Incomplete!
for(f = 1, f <= NItems(files), f++,
thisTable = openMyCSV(dir||files[f]);
thisTable << newColumn("x", Numeric, Continuous, Formula(36 + -19.5 * :items + 49.99 *:items* :items* :items));
);