导入多个相同格式的csv文件我有几个类似格式的 csv 文件。 所有 csv 文件在行号上都有标题。 279 & 数据从第 281 行开始。 尽管数据的长度可能会有所不同,但所有 csv 文件的最后 4 行都有不相关的信息,需要删除。
目前,我正在使用“多文件导入”功能来导入数据,但我不知道如何排除最后 4 行。 我在 JMP 表中导入后删除了行。 是否可以创建一个脚本来自动导入目录中的所有 csv 文件并通过排除最后 4 行来连接它们。 下面的脚本是我导入单个 csv 文件时得到的脚本。
Open("C:\Users\TEST\ABC.csv",
columns(
New Column( "Record", Character, "Nominal" ),
New Column( "14", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "ID", Character, "Nominal" ),
New Column( "XREL", Character, "Nominal" ),
New Column( "YREL", Character, "Nominal" ),
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, Space, 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( 1 ),
Column Names Start( 279 ),
Data Starts( 281 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
)实际上,所有列都包含连续格式的数值。 但是,上述无关信息使其被检测为“名义”。
分析数据表脚本编写回复:导入多个相同格式的 csv 文件这是对您的代码的修改,它查找给定目录中的所有文件,打开每个文件,删除最后四行,然后将其添加到串联文件中。
该代码未经过全面测试,因为我没有与您的输入匹配的文件。但它应该很接近,你应该能够解决它的任何小问题。
Names Default To Here( 1 );
dirFiles = Files In Directory( "C:\Users\TEST" );
// Create the output data table to concatenate all files to
dtFinal = New Table( "Final", New Column( "Source", character ) );
For( i = 1, i <= N Items( dirFiles ), i++,
dt = Open(
"C:\Users\TEST\" || dirFiles[i],
columns(
New Column( "Record", Character, "Nominal" ),
New Column( "14", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "ID", Character, "Nominal" ),
New Column( "XREL", Character, "Nominal" ),
New Column( "YREL", Character, "Nominal" ),
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, Space, 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( 1 ),
Column Names Start( 279 ),
Data Starts( 281 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
// Delete the last 4 rows
dt << delete rows( Index( N Rows( dt ) - 4, N Rows( dt ) ) );
// Add a source column
dt << New Column( "Source", character, set each value( dirFiles[i] ) );
// Concatenate the file
dtFinal << concatenate( dt, Append to first table( 1 ) );
// delete the current input table
Close( dt, nosave );
);
这将比多文件导入运行得慢,但它会完成工作
Re: 导入多个相同格式的csv文件谢谢吉姆。
您的解决方案为我节省了很多时间。