轉置標頭資料並連接文件我有一組原始數據 csv 文件,標題中包含元數據,我想將其連接並將標題數據複製到列中。 我已經能夠修改我在該網站上找到的“combine txttables.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我認為循環因表命名而停止。 如果我註解掉“關閉(數據表(“標題表”),NoSave);”它正確導入第一個檔案然後停止。 如果我取出註釋,我會得到一個資料表,其中包含按預期匯入的第一組原始資料和標頭資料以及第二個資料檔案的缺少標頭資料的原始資料。 它永遠不會超越第二個文件。 下面的範例顯示了原始資料檔案的格式,其中元資料位於第 1 行到第 3 行,原始資料從第 5 行開始:
| 測試週期 | 1 | | |
| 實驗室 | 實驗室_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 |
回覆:轉置標頭資料並連接文件@mlipsitz
您的感謝回覆不適合標記為「解決方案」。讀者需要有一個指向實際包含解決方案的回應的指標。請選擇顯示解決方案的回覆。
回覆:轉置標頭資料並連接文件非常感謝伊恩_jmp和摩根0 。 我同意我正在努力解決一些語法問題並嘗試組合來自不同來源的程式碼。 我能夠得到摩根0的程式碼需要進行一些細微的調整。 請參閱隨附的腳本。 是的,我的所有文件都採用自動資料擷取系統的相同格式。 再次感謝您對此的幫助以及文法課程!
回覆:轉置標頭資料並連接文件沒問題 - 我的許多失敗之一是我發現很難使用別人的程式碼:( 。 。 。
回覆:轉置標頭資料並連接文件對不起@ian_jmp ,你一定是在我編輯貼文時發布了你的腳本。
回覆:轉置標頭資料並連接文件@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");
我使用您顯示的文件以及該文件的修改測試了該腳本。 這是使用上面腳本的結果。

回覆:轉置標頭資料並連接文件使用訊息底部的格式,以下是一些將在(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");