헤더 데이터 전치 및 파일 연결헤더 데이터를 열에 연결하고 복사하려는 헤더에 메타데이터가 있는 원시 데이터 csv 파일 세트가 있습니다. 이 사이트에서 찾은 "combine txt tables.jsl" 스크립트를 수정하여 헤더 데이터를 가져오고 이를 바꾸고 원시 데이터 테이블에 조인할 수 있었습니다. 헤더 데이터는 2행에서 29행까지의 각 원시 데이터 파일에 포함되어 있으며 첫 번째 열을 열 이름으로 만들고 두 번째 열을 메타데이터로 변환하여 각 원시 데이터 행 아래로 복제합니다. 어떤 이유로 첫 번째 파일만 가져오고 가져오기 루프를 계속하지 않습니다. 내 스크립트는 다음과 같습니다.
names default to here (1);
Clear Symbols();
//set up variables with starting values
filelist={}; //empty list
//launch dialog to get the file location
prefilepath = munger(Pick Directory( "Browse to the Directory of the .txt / .csv files " ),1,"/","");
filepath = Convert File Path( prefilepath, Windows );
prefilelist = Files In Directory( filepath );
n2=nitems(prefilelist);
//filter out any non-txt or csv files
For( i2 = 1, n2 >= i2, i2++,
file=(prefilelist[i2]);
// changed from item 2 to item -1
If( Item( -1, prefilelist[i2], "." ) == "txt" | Item( -1, prefilelist[i2], "." ) == "csv",
Insert Into( filelist,file),
show(file)
)
);
//filter out any non-Excel files for the file list.
For( i2 = 1, n2 >= i2, i2++,
file=(prefilelist[i2]);
If( Item( -1, prefilelist[i2], "." ) == "xls" | Item( -1, prefilelist[i2], "." ) == "xlsx",
Insert Into( filelist,file),
show(file)
)
);
//Create raw data table:
nf=nitems(filelist); //number of items in the working list
For( iii = 1 , iii <= nf, iii++, //this starts the first loop
filenow = ( filelist[iii] );
fileopen=(filepath||filenow);
dt=open(fileopen,private,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, 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( 1 ),
Data Starts( 2 ),
Lines To Read( 29 ),
Year Rule( "20xx" )
)
);
//Transpose header data into new columns and create "Transposed Header Table"
dt<<( "Header table" ) << Transpose(
columns( :TTV0.000000 ),
Label( :Name( "Test Name:" ) ),
Output Table( "Transposed Header table" )
);
//New Column( "Source", Character, Nominal );
//:Source << set each value( filenow );
dt=open(fileopen,private,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, 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( 32 ),
Data Starts( 33 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
New Column( "Source", Character, Nominal );
:Source << set each value( filenow );
//Merge header table with raw data into "Combined Data Table"
dtjoined=dt<< Join(
With( Data Table( "Transposed Header table" ) ),
Cartesian Join
);
//add the current table to the bottom of the combined data table
dtjoined << Concatenate( Data Table( dt ), Append to first table );
dtjoined << Set Name("Combined Data Table");
Close(Data Table("Transposed Header table"),NoSave);
Close(Data Table("Header table"),NoSave);
dt << Run Formulas();
);//end of the first for loop테이블 이름 지정 때문에 루프가 중지된 것 같습니다. "Close(Data Table("Header table"),NoSave);"를 주석 처리하면 스크립트는 첫 번째 파일을 올바르게 가져온 다음 중지합니다. 주석을 제거하면 예상대로 가져온 원시 데이터 및 헤더 데이터의 첫 번째 세트와 헤더 데이터가 누락된 두 번째 데이터 파일의 원시 데이터 가치가 있는 데이터 테이블이 생성됩니다. 두 번째 파일을 넘어서지 않습니다. 다음은 1행에서 3행까지의 메타데이터와 5행에서 시작하는 원시 데이터로 원시 데이터 파일의 형식이 지정되는 방법을 보여주는 예입니다.
| 테스트 주기 | 1 | | |
| 실혐실 | 연구실_A | | |
| 일련 번호 | 123ABC | | |
| 시간 | 메트릭 A | 메트릭 B | 미터법 C |
| 1 | 1 | 70 | 60 |
| 2 | .1 | 71 | 61.1 |
| 삼 | .2 | 73 | 62.5 |
| 4 | .삼 | 78 | 63.8 |
| 5 | .4 | 80 | 65.7 |
| 6 | .5 | 85 | 69.2 |
| 7 | .6 | 89 | 72.3 |
Re: 헤더 데이터를 바꾸고 파일을 연결합니다.@mlipsitz
귀하의 감사 응답은 "솔루션"으로 태그를 지정하기에 적절한 항목이 아닙니다.판독기는 실제로 솔루션을 포함하는 응답에 대한 포인터가 있어야 합니다.솔루션을 보여주는 응답을 선택하십시오.
Re: 헤더 데이터를 바꾸고 파일을 연결합니다.매우 감사합니다 ian_jmp 그리고 gzmorgan0 . 나는 일부 구문으로 어려움을 겪고 있었고 다양한 소스의 코드를 결합하려고 노력했다는 데 동의합니다. 나는 얻을 수 있었다 gzmorgan0 약간의 조정으로 작동하는 의 코드입니다. 첨부된 스크립트를 참조하세요. 예, 모든 파일은 자동화된 데이터 수집 시스템에서 동일하게 포맷됩니다. 이에 대한 도움과 구문 강의에 다시 한 번 감사드립니다!
Re: 헤더 데이터를 바꾸고 파일을 연결합니다.문제 없습니다. 제 많은 실패 중 하나는 다른 사람의 코드로 작업하기가 어렵다는 것입니다.
. . .
Re: 헤더 데이터를 바꾸고 파일을 연결합니다.죄송합니다 @ian_jmp , 내가 게시물을 편집하는 동안 스크립트를 게시했음에 틀림없습니다.
Re: 헤더 데이터를 바꾸고 파일을 연결합니다.@mlipsitz , 이 스크립트에는 여러 가지 문제가 있습니다. 기본 JSL 구문을 배우는 데 더 많은 작업이 필요하다고 생각합니다.예, 우리 모두는 이미 작성된 코드를 가져와 수정하여 언어를 배우지만 몇 가지 기본 사항이 필요합니다.예를 들어 스크립트의 이 부분은 테이블 dt의 이름을 바꾸려는 것처럼 보입니다. 열이 있는 코드에는 ::TTV0.000000이 정의되어 있으며 이 조옮김은 ~ 아니다 내가 원한다고 생각하는 것을하십시오.
//Transpose header data into new columns and create "Transposed Header Table"
dt<<( "Header table" ) << Transpose(
columns( :TTV0.000000 ),
Label( :Name( "Test Name:" ) ),
Output Table( "Transposed Header table" )
);
또한 아래 코드를 실행하면 dt는 데카르트 조인 전 원시 데이터 값의 테이블이고 dtjoined는 레이블이 있는 값의 테이블입니다. 테이블을 덮어쓰고 있습니다(이름 충돌).
//add the current table to the bottom of the combined data table
dtjoined << Concatenate( Data Table( dt ), Append to first table );
다음은 몇 가지 제안 사항입니다.
- 스크립트가 작성되고 제대로 실행될 때까지 테이블을 비공개로 만들지 마십시오.
- dt는 테이블 참조의 줄임말입니다. 변수 이름을 재사용(용도 변경)하는 것은 괜찮지만 이전 테이블이 닫힐 때까지 테이블에 대해서는 권장하지 않습니다. 예를 들어 코드를 더 읽기 쉽게 만드는 변수 이름을 사용하십시오. dthdr 헤더 테이블 및 dtx 전치된 테이블의 경우.
- 더 이상 필요하지 않으면 즉시 테이블을 닫습니다.
- 정말 하고 싶니? 파일 결합 또는 사슬 같이 잇다 파일? 각 파일의 메트릭이 동일하면 연결을 원한다고 생각합니다.
- 몇 개의 파일로 스크립트를 테스트하십시오. 즉, 펜스를 향해 스윙하기 전에 기본 안타를 얻는 방법을 배우십시오.
귀하의 읽기 문은 귀하의 샘플 테이블에 적합하지 않습니다.각 파일의 형식이 동일합니까?아니면 원시 데이터가 시작되는 위치를 결정하기 위해 패턴을 검색해야 합니까?이 스크립트는 파일 형식이 고정되어 작동합니다. 원시 데이터 행의 수는 변경될 수 있습니다.테스트할 파일 목록을 하드코딩했습니다. 파일을 가져오는 코드는 괜찮아 보입니다.
Names default to Here(1);
filelist = {"example1_20190214.txt", "example2_20190214.txt"};
filepath = "c:\temp\";
df = nitems(filelist);
for(iii=1, iii<=df, iii++,
filenow = filelist[iii];
fileopen=(filepath||filenow);
dthdr=open(fileopen,//private,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, 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(0 ),
Column Names Start(0 ),
Data Starts( 1 ),
Lines To Read( 3 ),
Year Rule( "20xx" )
)
);
nc = ncol(dthdr);
dthdr << Combine Columns( Delimiter(" "), Columns(2::nc), Column Name("Info") );
dthdr << delete columns(3::nc);
//Transpose header data into new columns and create "Transposed Header Table"
dthdr<< set name( "Header table" );
dtx = dthdr << Transpose(
columns( :Info ),
Label( Column(1) ),
Output Table( "Transposed Header table" )
);
dtx << delete column (:Label);
//Close Header table
Close(dthdr,Nosave);
//New Column( "Source", Character, Nominal );
//:Source << set each value( filenow );
dt=open(fileopen,//private,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, 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( 4 ),
Data Starts( 5 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
New Column( "Source", Character, Nominal );
:Source << set each value( filenow );
//Merge Transposed Header table, dtx, with raw data table, dt
dtjoined=dt<< Join(
With( dtx ),
Cartesian Join
);
//close Transposed Header table
Close(dtx, NoSave);
//close raw data table, dt
Close(dt, NoSave);
//For the first file, keep it, it will be used to append other file
//For the other files, append then close
If (iii==1 , dtfinal = dtjoined,
//else
current data table(dtfinal);
dtfinal << Concatenate( dtjoined , Append to first table(1));
wait(0);
close(dtjoined, NoSave)
);
//no need for Run Formulas() there are no formulas in the table
); //end the for iii loop
//Give the final table a name
dtfinal << set name( "Combined Data Table");
표시한 파일과 해당 파일을 수정하여 이 스크립트를 테스트했습니다. 다음은 위의 스크립트를 사용한 결과입니다.

Re: 헤더 데이터를 바꾸고 파일을 연결합니다.다음은 메시지 하단의 형식을 사용하여 데스크탑(Mac)에서 .CSV 파일을 처리하는 코드입니다.
NamesDefaultToHere(1);
// Get a list of .csv files on the desktop
fileList = FilesInDirectory("$DESKTOP");
for(f=NItems(fileList), f>=1, f--, If(!EndsWith(Uppercase(fileList[f]), ".CSV"), RemoveFrom(fileList, f)));
fileList;
// Process each .csv file
For(f=1, f<=NItems(fileList), f++,
// Get metadata (generated by JMP)
dtm = Open(
"$DESKTOP/"||fileList[f],
columns(
New Column( "Name", Character, "Nominal" ),
New Column( "Value", Character, "Nominal" ),
),
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 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( 3 ),
Year Rule( "20xx" )
),
Invisible
);
// Get data (generated by JMP)
dtd = Open(
"$DESKTOP/"||fileList[f],
columns(
New Column( "Time", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "Meric A", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "Metric B", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "Metric C", 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( 4 ),
Data Starts( 5 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
),
Invisible
);
// Add the values from dtm to dtd
for (c=1, c<=NRow(dtm), c++,
vals = {}; for(r=1, r<=NRow(dtd), r++, InsertInto(vals, Column(dtm, 2)[c]));
dtd << newColumn(Column(dtm, 1)[c], Character, Values(vals));
);
Close(dtm, NoSave);
// Append tables
if(f==1,
dtFinal = Eval(dtd << GetScript),
dtFinal << Concatenate(dtd, AppendToFirstTable);
);
Close(dtd, NoSave);
);
// Tidy up dtFinal
dtFinal << ShowWindow(1);
dtFinal << setName("Final Data");
dtFinal << deleteProperty("Source");