连接具有不同列顺序的多个表球队,
我有5个csv文件,每个文件有200列(不是每个文件有相同的列数)。每个文件都有不同顺序的列。我想写一个scirpt,让用户选择所有5个文件,然后组合所有5个文件(删除不匹配的列)并将组合的csv文件保存到所需的位置。我想使用类似命令(data = Open(选择文件,“选择文件”,x)),x是目录),来选择文件。
谢谢
Re:连接具有不同列顺序的多个表谢谢 @txnelson
你总是很有帮助。Re:连接具有不同列顺序的多个表下面是脚本的返工,选择要在一个选择中连接的所有文件,然后运行一个小脚本将它们放在一起,只保留找到匹配的列。 我已根据您的规范构建它,但可以轻松更改它以允许连接任意数量的文件
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 );
Re:连接具有不同列顺序的多个表 @Rajat ,
我不明白为什么你需要创建一个新表。我会做这样的事情。
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); 列表为您提供排序顺序和原始顺序。另外,请分享您到目前为止所尝试的内容,以便更容易看到您的目标。我无法完全理解你到目前为止所尝试的内容以及你失败的地方。如果您无法共享数据,请尝试创建一些示例数据,以便每个人都可以轻松地重新创建问题并帮助指导您。
Re:连接具有不同列顺序的多个表 @uday_guntupalli
可以帮我编写语法来保存原始订单并使用保存的订单重新排序最终表吗?
谢谢Re:连接具有不同列顺序的多个表 @Rajat ,
dt << Reorder By Name();
Re:连接具有不同列顺序的多个表谢谢 @uday_guntupalli
如果我们可以重新排序列并使它们具有相同的顺序。之后我们将连接所有表。
你能帮我写一些语法来根据第一个表重新排序所有表的列,然后我们将在循环中使用concatenate命令。如果不可能,我们可以保存第一个表的原始列顺序,然后用它们的名称重新排序所有表,然后在循环中连接。最后,我们将根据先前保存的列顺序对最终表重新排序。
@Craige_Hales @txnelson 你有解决这个问题的方法吗?
谢谢。感谢你的帮助。Re:连接具有不同列顺序的多个表 @Rajat ,
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
这是我的例子中的拼写错误。选择(CommonItems)是合适的。
我不知道Concatenate是否有效,欢迎您试试
Re:连接具有不同列顺序的多个表 @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>
就像我在“ ThisListAsAA “,” ThisList “将作为关联数组存储。请帮助理解下两行。 - 在循环中加入表格,请您解释以下几行。
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
没有列的列名:name,:age等,手动我们无法写入选择列,因为数量很大。另外我们不能通过行号加入,因为行数不同,我们想要连接表而不是连接。
谢谢
Re:连接具有不同列顺序的多个表 @Rajat ,
我正在做一个假设(我不知道它有多有效)你有相同的行数。
// 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
);
);
); Re:连接具有不同列顺序的多个表谢谢Uday。我想连接常见的列。Re:连接具有不同列顺序的多个表 @Rajat ,
看看你所描述的问题,让我们将其分解为多个步骤。
第1步:选择所需的文件 - 使用选择文件()
),
"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文件中的公共列?