我正在嘗試編寫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 文件進行編輯的時間!