パラメータ名と公差を含む CSV ファイルがあります。 生データを含む別のテキスト ファイルがあります。 CSVファイルとテキストファイルの両方を読み取り、CSVファイルのスクリーニング基準をテキストファイルのデータに適用するスクリプトをJSLで作成しようとしています。
CSV ファイル形式は次のとおりです。
# パラメータの許容範囲
1 身長 30
2 直径 50
TXTファイル形式は
ID 高さ 直径
1AA 50 20
1BB 20 30
審査基準を満たさなかった ID のテキスト ファイルを作成したい。
たとえば、上記のケースでは、値 50 が許容値 30 より大きいため、1AA は高さスクリーニングに失敗します。 1AAが書かれたテキストファイルが欲しいです。
どんなガイダンスも高く評価されます。
これまでのところ、CSV ファイルまたはテキスト ファイルを開く方法しか知りませんが、許容範囲を変数に保存し、スクリーニング条件をデータに適用する方法を理解することはできません。
txt_path = Pick File( "Pick a file" );
dt_screen = Open(
txt_path,
Column Names are on line( 1 ),
End of Field( "Comma" ),
Data Starts on Line( 2 ),
End Of Line( "CRLF" )
);
どうもありがとう。 あなたのソリューションは、私が必要としていた方法で完全に機能します。 行と列が多い大規模なデータセットで使用しようとしています。 うまくいけば、スクリプトをすばやく変更できるようになります。 よろしくお願いいたします。
遅くなりましたが、私が思いついたのはこれです。 Jim はテキスト ファイル出力に関する部分をキャッチしましたが、私はそれを見逃していました...
csvfile = Save Text File("$temp/x.csv",
"# Parameter Tolerance
1 Height 30
2 Diameter 50
"
);
txtfile = Save Text File("$temp/y.txt",
"ID Height Diameter
1AA 50 20
1BB 20 30
1cc 20 90
"
);
dtparms = Open(csvfile,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Spaces, Space, CSV( 0 ) ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 2 )
)
);
// get the parameters
parameterNames = dtparms[0, "parameter"]; // {"Height", "Diameter"}
parameterValues = dtparms[0, "tolerance"]; // [30, 50]
dtdata = Open(txtfile,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Spaces, Space, CSV( 0 ) ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 2 )
)
);
For( iPname = 1, iPname <= N Items( parameterNames ), iPname += 1,
col = Column( parameterNames[iPname] ); // Column( "Diameter" )
val = parameterValues[iPname]; // 50
dtdata << selectwhere( dtdata:col > val, current selection( "extend" ) );
);
dtdata<<SELECT columns=""></SELECT>これは、サンプル データで動作するスクリプトであり、より大きなデータ テーブルで動作するはずです。
Names default to here(1);
// Open the data file
txt_path = Pick File( "Pick a Data File" );
dt_data = Open(
txt_path,
columns(
New Column( "ID", Character, "Nominal" ),
New Column( "Height", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "Diamiter", Numeric, "Continuous", Format( "Best", 12 ) )
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, Comma, CSV( 1 ) ),
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( 1 ),
Data Starts( 2 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
// Open the Tolerance data
txt_path = Pick File( "Pick a Tolerance File" );
dt_Tolerance = Open(
txt_path,
Worksheets( "Sheet1" ),
Use for all sheets( 1 ),
Concatenate Worksheets( 0 ),
Create Concatenation Column( 0 ),
Worksheet Settings(
1,
Has Column Headers( 1 ),
Number of Rows in Headers( 1 ),
Headers Start on Row( 1 ),
Data Starts on Row( 2 ),
Data Starts on Column( 1 ),
Data Ends on Row( 0 ),
Data Ends on Column( 0 ),
Replicated Spanned Rows( 1 ),
Replicated Spanned Headers( 0 ),
Suppress Hidden Rows( 1 ),
Suppress Hidden Columns( 1 ),
Suppress Empty Columns( 1 ),
Treat as Hierarchy( 0 ),
Multiple Series Stack( 0 ),
Import Cell Colors( 0 ),
Limit Column Detect( 0 ),
Column Separator String( "-" )
)
);
// Get the column names in the data file
colNames = dt_data << get column names(string,continuous);
// Loop across the columns and get the Tolerances and then go through all
// of the rows in the data table and select the ones that fail
For( theCol = 1, theCol <= N Items(colNames), theCol++,
theToleranceRow = dt_Tolerance << get rows where( colNames[theCol] == dt_Tolerance:Parameter);
If( N Rows( theToleranceRow ) >0,
// Change theToleranceRow from a matrix to a scaler value
theToleranceRow = theToleranceRow[1];
For(theRow = 1, theRow <= N Rows( dt_data ), theRow++,
If( column(dt_data,colNames[theCol])[theRow] > dt_Tolerance:Tolerance[theToleranceRow],
current data table(dt_data);
Row State( theRow ) = Selected State( 1 );
)
)
)
);
// Create the text string to save to the text file for all failing IDs
failedRows = dt_data<<GET selected="" rows=""> 0,
failedText = dt_data:ID[failedRows[1]];
For( selectedRow = 2, selectedRow <= N Rows(failedRows), selectedRow++,
failedText = failedText || "\!n" || dt_data:ID[failedRows[selectedRow]]
)
);
// Save the failed text string to a file
Save Text File( "path\to\failed.txt", failedText );</GET>