我正在尝试编写 JSL 来提取多个文件并将它们连接在一起,但有些文件的标题从第 2 行开始,有些文件的标题从第 3 行开始。我不知道哪些文件的标题位于不同的行,我试图避免打开所有文件来查找。有没有办法编写 JSL 来查找第一个列标题(时间)并从那里开始读取?
类似 CSV 的文件? 开头多出来的几行能不能容易识别? 都是空白? 标准关键字? 您可以使用脚本读取文件的前 N 个字节并决定 CSV 导入应跳过多少行。 查看 LoadTextFile 函数进行初步打开。
是的,它们是 CSV 文件。有些第一行是日期,第二行是 VIN,然后是标题。其他人的第一行是日期,然后是标题。您可以发布您所指脚本的示例吗?
像这样的东西。
// pick a test case...
f = savetextfile("$temp\deleteme.csv","1jan2000\!ntime,x,y,z\!n101,1,2,3\!n101,4,5,6");
//f = Save Text File( "$temp\deleteme.csv", "1jan2000\!nVIN\!ntime,x,y,z\!n102,1,2,3\!n102,4,5,6" );
text = Load Text File( f );
// count newlines before "time"
newline = "\!n\!r" | "\!r\!n" | "\!n" | "\!r";
n = 0;
Pat Match( // there are a lot of ways you could count newlines before "time"
text, // use the pattern matcher to loop through the text
Pat Pos( 0 ) +
Pat Repeat(
("time" + Pat Rem()) // found "time", skip to the end
| // OR...skip to the end of the line and count it
(Pat Break( "\!n\!r" ) + newline + Pat Test( n += 1; 1; ))
)
);
Show( n );
HEADERLINE = n + 1;
FIRSTDATALINE = n + 2;
Open(
f,
columns(
New Column( "time", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "x", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "y", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "z", 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( 1 ),
Column Names Start( HEADERLINE ),
First Named Column( 1 ),
Data Starts( FIRSTDATALINE ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
太感谢了! 这对我来说非常有用,为我节省了很多打开所有 CSV 文件进行编辑的时间!