ヘッダー データの転置とファイルの連結ヘッダーにメタデータを含む生データcsvファイルのセットがあり、ヘッダーデータを連結して列にコピーします。 このサイトで見つけた "combine txt tables.jsl" スクリプトを変更して、ヘッダー データを取得し、転置して、生データ テーブルに結合することができました。 ヘッダー データは、行 2 ~ 29 の各生データ ファイルに含まれています。これを転置して、最初の列を列名に、2 番目の列をメタデータにして、各生データ行に複製します。 何らかの理由で、最初のファイルのみを取り込み、インポート ループを続行しません。 私のスクリプトは次のとおりです。
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);」をコメントアウトすると、スクリプトは、最初のファイルを正しくインポートしてから停止します。 コメントを削除すると、期待どおりにインポートされた生データとヘッダー データの最初のセットと、ヘッダー データが欠落している 2 番目のデータ ファイルの生データに相当するデータ テーブルが得られます。 その 2 番目のファイルを超えることはありません。 次の例は、生データ ファイルがどのようにフォーマットされているかを示しています。メタデータは行 1 ~ 3 にあり、生データは行 5 から始まります。
| テストサイクル | 1 | | |
| ラボ | Lab_A | | |
| シリアルナンバー | 123ABC | | |
| 時間 | メトリック A | メトリック B | メトリック C |
| 1 | 1 | 70 | 60 |
| 2 | .1 | 71 | 61.1 |
| 3 | .2 | 73 | 62.5 |
| 4 | .3 | 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: ヘッダー データを転置してファイルを連結する問題ありません - 私の多くの失敗の 1 つは、他の人のコードを操作するのが難しいことです
. . .
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");