매개변수 이름과 공차가 포함된 CSV 파일이 있습니다. 원시 데이터가 포함된 다른 텍스트 파일이 있습니다. CSV 파일과 텍스트 파일을 모두 읽은 다음 CSV 파일의 심사 기준을 텍스트 파일의 데이터에 적용하기 위해 JSL로 스크립트를 작성하려고 합니다.
CSV 파일 형식은 다음과 같습니다.
# 매개변수 공차
1 높이 30
2 직경 50
TXT 파일 형식은
ID 높이 직경
1AA 50 20
1BB 20 30
심사 기준에 실패한 ID로 텍스트 파일을 만들고 싶습니다.
예를 들어 위의 경우 1AA는 값 50이 허용 오차 30보다 크기 때문에 높이 선별에 실패합니다. 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>