連接具有不同列順序的多個表團隊,
我有 5 個 csv 文件,每個文件都有 200 列(並非每個文件都有相同的列數)。 每個文件都有不同順序的列。 我想編寫一個腳本,讓用戶選擇所有 5 個文件,然後合併所有 5 個文件(刪除不匹配的列)並將合併的 csv 文件保存到所需位置。 我想使用類似於 (data = Open(Pick File("Choose the File", x)), x 是目錄) 的命令來選擇文件。
謝謝
回复:連接具有不同列順序的多個表謝謝@tx尼爾森
你總是一個很大的幫助。回复:連接具有不同列順序的多個表這是腳本的重寫,選擇要在一個選擇中連接的所有文件,然後運行一個小腳本將它們放在一起,只保留找到匹配的列。 我已根據您的規格構建了它,但是可以輕鬆更改它以允許連接任意數量的文件
Names Default To Here( 1 );
inputDirectory = "C:\";
// Select the files to be read in......this can be any number of files
filePath = Pick File(
"Choose the File to be Concatenated to:",
inputDirectory,
{"CSV Files|csv;", "All Files|*"},
1,
0,
""
);
// Open the first file
dtMain = Open( filePath, invisible );
files = Pick File(
"Choose the Additional 4 Files to Read In:",
inputDirectory,
{"CSV Files|csv;", "All Files|*"},
1,
0,
"",
multiple
);
// Validate that files have been read in
If( Try( N Items( files ), 0 ) == 4,
Dialog( "Incorrect number of files were read in."
"Only 4 additional files can be read in");
Throw( "Incorrect number of files were read in" );
);
// Read in the remaining files and process them
For( i = 2, i <= N Items( Files ), i++,
Try( dtConCat = Open( Files[i], invisible ) );
mainColList = dtMain << Get Column Names( string );
conCatColList = dtConCat << Get Column Names( string );
// Delete the columns in the dtConCat table that do not exist in the dtMain table
delList = {};
For( k = 1, k <= N Items( conCatColList ), k++,
If( N Rows( Loc( mainColList, conCatColList[k] ) ) == 0,
Insert Into( delList, conCatColList[k] )
)
);
If( N Items( delList ) > 0,
dtConCat << delete columns( delList )
);
// Delete the columns in the dtMain table that were not found in the dtConCat table
delList = {};
For( k = 1, k <= N Items( mainColList ),
k++,
If( N Rows( Loc( conCatColList, mainColList[k] ) ) == 0,
Insert Into( delList, mainColList[k] )
)
);
If( N Items( delList ) > 0,
dtMain << delete columns( delList )
);
// If any columns still remain in the two tables, then concatenate them together
If( N Cols( dtMain ) > 0 & N Cols( dtConCat ) > 0,
dtMain = dtMain << Concatenate( dtConCat, Append to First Table )
);
// Close the no longer needed file
Close( dtConCat, nosave );
);
// Make the main file visible
dtMain << show window( 1 );
回复:連接具有不同列順序的多個表@拉賈特,
我不明白為什麼你需要創建一個新表。 我會做這樣的事情。
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
OriginalColOrder = dt << Get Column Names("string");
dt << Reorder By Name();
SortedColOrder = dt << Get Column Names("string");
Show(OriginalColOrder);
Show(SortedColOrder); 這些列表為您提供排序順序和原始順序。 另外,請分享您到目前為止所嘗試過的內容,以便更容易了解您的前進方向。 我無法準確理解您到目前為止所嘗試過的以及失敗的地方。 如果您無法共享數據,請嘗試創建一些示例數據,以便每個人都可以輕鬆重現問題並幫助指導您。
回复:連接具有不同列順序的多個表@uday_guntupalli
可以幫我編寫語法來保存原始順序並使用保存的順序對最終表重新排序嗎?
謝謝回复:連接具有不同列順序的多個表@拉賈特,
dt << Reorder By Name();
回复:連接具有不同列順序的多個表謝謝@uday_guntupalli
如果我們可以重新排序列並使它們具有相同的順序。 之後我們將連接所有表。
你能幫我編寫語法來根據第一個表重新排序所有表的列,然後我們將在循環中使用連接命令。 如果不可能,我們可以保存第一個表的原始列順序,然後用它們的名稱對所有表重新排序,然後在循環中連接。 最後,我們將根據之前保存的列順序對最終表重新排序。
@Craige_Hales @tx尼爾森你有解決這個問題的方法嗎?
謝謝。 感謝您的幫助。回复:連接具有不同列順序的多個表@拉賈特,
ThisListAsAA = Associative Array(ThisList);
ThisListAsAAKeys =ThisListAsAA << Get Keys;
If(i == 1,
CommonItems = ThisListAsAAKeys;
,
// else
CommonItems << Intersect(ThisListAsAAKeys);
);
關聯數組具有一對一的關係或映射。 因此,一旦我們構造了關聯數組,第二行就會嘗試提取關聯數組映射中的唯一鍵。 至於 If 塊,請考慮集合論,當 i = 1 時,我們將關聯數組 1 的唯一鍵分配給結果。 從那時起,我們反復將其與其他關聯數組相交,最終得到一個列表,該列表僅包含所有關聯數組中共有的元素。
Select( :name, :age, :sex, :height, :weight ),
By Row Number
這是我的示例中的一個拼寫錯誤。 Select(CommonItems) 是合適的。
不知道Concatenate行不行,歡迎嘗試
回复:連接具有不同列順序的多個表@uday_guntupalli
感謝您的大力幫助。
我的評論很少
- 所有表都有不同的行數。
- 我想連接(最終表中的行數將是單個表的行數之和)不連接表的表。 連接循環的工作原理是否相同?
另外,您能幫我理解以下命令嗎?
- 在循環中查找公共列名稱。 您能解釋一下以下幾行嗎?
ThisListAsAA <SPAN class="token operator">=</SPAN> <SPAN class="token function">Associative Array</SPAN><SPAN class="token punctuation">(</SPAN>ThisList<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
ThisListAsAAKeys <SPAN class="token operator">=</SPAN>ThisListAsAA <SPAN class="token operator"><</SPAN><SPAN class="token operator"><</SPAN><SPAN class="token messages"> Get Keys</SPAN><SPAN class="token punctuation">;</SPAN>
<SPAN class="token function">If</SPAN><SPAN class="token punctuation">(</SPAN>i <SPAN class="token operator">==</SPAN> <SPAN class="token number">1</SPAN><SPAN class="token punctuation">,</SPAN>
CommonItems <SPAN class="token operator">=</SPAN> ThisListAsAAKeys<SPAN class="token punctuation">;</SPAN>
<SPAN class="token punctuation">,</SPAN>
<SPAN class="token comment">// else </SPAN>
CommonItems <SPAN class="token operator"><</SPAN><SPAN class="token operator"><</SPAN><SPAN class="token messages"> Intersect</SPAN><SPAN class="token punctuation">(</SPAN>ThisListAsAAKeys<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
據我了解“此列表為 AA “,”此列表" 將被存儲為關聯數組。 請幫助理解接下來的兩行。 - 在連接表格的循環中,您能解釋一下以下幾行嗎?
Select<SPAN class="token punctuation">(</SPAN> <SPAN class="token punctuation">:</SPAN>name<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>age<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>sex<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>height<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>weight <SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">,</SPAN>
By Row Number
沒有帶有列名的列:名稱,:年齡等,並且我們無法手動寫入選擇列,因為數量巨大。 另外,我們不能按行號連接,因為行數不同,並且我們想要連接表而不是連接。
謝謝
回复:連接具有不同列順序的多個表@拉賈特,
我假設(我不知道它有多有效)您有相同的行數。
// Define Your Directory and a list to hold File Paths
MyDirectory = "C:\" ;
nFiles = 5;
MyFilePaths = {};
ColNamesListOfList = {};
OpenDts = {};
// Test if Directory exists and get all file paths
Try( If(Directory Exists(MyDirectory),
for(i = 1 , i <= nFiles, i ++,
FilePath = Pick File( "Select File", MyDirectory);
Insert Into(MyFilePaths,FilePath);
);
,
// else
Throw()
),
"Unable to Open File";
);
// Get List of All Column Names
for(i = 1, i <= nFiles, i++,
OpenDts[i] = Open(MyDirectory || "\" || FilePath[i],"Invisible");
ColNames = OpenDts[i] << Get Column Names("String");
Insert Into(ColNamesListOfList,ColNames);
);
// Find Common Column Names
for(i = 1, i <= nFiles, i++,
ThisList = ColNamesListOfList[i];
ThisListAsAA = Associative Array(ThisList);
ThisListAsAAKeys =ThisListAsAA << Get Keys;
If(i == 1,
CommonItems = ThisListAsAAKeys;
,
// else
CommonItems << Intersect(ThisListAsAAKeys);
);
);
// Concatenate
for(i = 1, i <= nFiles, i++,
If(i == 1,
dt_Results = OpenDts[i];
,
dt_Results << Join(With( OpenDts[i]),
Merge Same Name Columns,
Select( :name, :age, :sex, :height, :weight ),
By Row Number // assuming that the tables have the same number of rows
);
);
); 回复:連接具有不同列順序的多個表謝謝烏代。 我想連接公共列。回复:連接具有不同列順序的多個表@拉賈特,
看看您所描述的問題,讓我們將其分解為多個步驟。
第 1 步:選擇所需的文件 - 使用 Pick File ()
),
"Unable to Open File";
); // Define Your Directory and a list to hold File Paths
MyDirectory = "C:\" ;
nFiles = 5;
MyFilePaths = {};
OpenDts = {};
// Test if Directory exists and get all file paths
Try( If(Directory Exists(MyDirectory),
for(i = 1 , i <= nFiles, i ++,
FilePath = Pick File( "Select File", MyDirectory);
Insert Into(MyFilePaths,FilePath);
); <BR /> , <BR /> // else<BR /> Throw()<BR />
第 2 步:循環遍歷文件,打開並連接公共列
在這裡,我有一個問題想問你。 最終表中是否有您期望的列列表(或者)它只是所有 5 個 csv 文件中的公共列嗎?