转置标头数据并连接文件我有一组原始数据 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 |
| 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
您的感谢回复不适合被标记为“解决方案”。读者需要有一个指向实际包含解决方案的响应的指针。请选择显示解决方案的响应。
回复:转置头数据并连接文件非常感谢ian_jmp和gzmorgan0 . 我同意我在一些语法上苦苦挣扎,并试图组合来自各种来源的代码。 我能够得到gzmorgan0的代码可以稍作调整。 请参阅随附的脚本。 是的,我所有的文件都通过自动数据采集系统进行了相同的格式化。 再次感谢您对此和语法课的帮助!
回复:转置头数据并连接文件没问题 - 我的许多失败之一是我发现很难使用其他人的代码:( . . .
回复:转置头数据并连接文件对不起@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");