我想将 txt 文件导入 JMP,但文本未完全对齐。 所以我想删除空单元格。 我尝试了很多不同的方法,但似乎JMP只能删除整个列/行。 有没有办法从输入格式转换为输出格式? 谢谢。
输入:
| 姓名 | 第1栏 | 第2栏 | 第3栏 | 第4栏 | 第5栏 |
| 艾玛 | | 38 | 33208 | 30630 | 31360 |
| 玛丽 | | 38 | 48263 | 44694 | 45753 |
| 丽莎 | 38 | 45961 | 42755 | 43766 | 43887 |
| 大卫 | 38 | 32799 | 30643 | 31365 | 31452 |
输出:
姓名 | 第1栏 | 第2栏 | 第3栏 | 第4栏 | 第5栏 |
艾玛 | 38 | 33208 | 30630 | 31360 | 30350 |
玛丽 | 38 | 48263 | 44694 | 45753 | 47263 |
丽莎 | 38 | 45961 | 42755 | 43766 | 43887 |
大卫 | 38 | 32799 | 30643 | 31365 | 31452 |
谢谢你! 效果很好! 这篇文章对我目前正在做的事情至关重要,这确实是一个很大的帮助。
如果您的文件不是很大并且您的 JMP 版本包含字符函数 Collapse Whitespace();
这是使用 JSL 的更简单的解决方案。
Names Default to Here(1);
infid = "c:\temp\ExtraTab_testIn.txt";
outfid = "c:\temp\_xxxdummy.txt";
fid = Load Text File(infid);
ww = words(fid, "\!N"); //each line is stored as text in a list
for(i=1, i<=nitems(ww), i++,
ww[i] = Collapse Whitespace(ww[i]); //replace tabs spaces, or any nonprinatble char even EOL with a single space
);
fid = Concat Items(ww, "\!N"); //combine into a single text and add the EOL char
Save Text File(outfid,fid); //save it
<BR />delete symbols(fid, ww);
//open it
dt = Open( outfid,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Space, 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( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
If( File Exists(outfid), Delete File(outfid) );
@艾玛蓝,Jim 提供了导入文本文件后如何解决问题的解决方案。 我将解释两个选项,每个选项都取决于您的原始文件。
#1 如果您的原始文件以空格作为列分隔符,则修复可能很容易。 当您选择文件>打开使用该选项带预览的数据并确保选择了 Space 和 Spaces

如果您选择了数据,使用文本导入首选项,您的默认值可能是空格。 如果是这种情况,请使用通过 UI 进行的修复带预览的数据。 如果您使用脚本,则应使用下面脚本中的 Open 语句。
#2如果您的原始文件使用制表符作为列分隔符,则修复并不那么容易。 通过 UI,您可以使用文本编辑器并使用查找和用空格替换制表符。我发现没有方法可以指定选项卡以防有额外的选项卡。 评语:一选项卡选项可能是一个不错的愿望清单项,或者允许用户指定多个字符,例如 0x090x09 或 HexToChar("0909")其他或添加一个选项忽略重复的分隔符。 如果您无法编辑该文件,或者不想使用文本编辑器将每个制表符替换为空格,并且您使用的是 Windows PC,则可以通过更改文件路径来修改下面的 JSL 文件。 如果您的名称列有空格,这将不起作用,并且可以使用使用正则表达式的不同脚本将重复的选项卡替换为单个选项卡。
如果您将数据复制并粘贴到表中,那么您应该使用吉姆的解决方案。
如果您有有关输入文件的更多详细信息,可以提供更有用的脚本/解决方案。
Names Default to Here(1);
findchar = "\!t"; //tab
infid = "C:\temp\ExtraTab_testIn.txt"; //file path
outfid = "c:\temp\_xxxdummy.txt";
//This was DOS script was found at StackOverflow 1/16/2019
//https://stackoverflow.com/questions/23075953/batch-script-to-find-and-replace-a-string-in-text-file-without-creating-an-extra/23076141
//It replaces the tabs in the infid with spaces and writes it to a outfid with spaces.
//If you want to replace the original file's tabs do the following
// uncomment lines 29 and 30 below: "del %textfile"" and "rename %newfile %textfile"
// change the Open( outfid, ..) to Open(infid, ...) in line 38
//To keep outfid, don't make the changes described above. But comment or delete the last JSL statement
dos_cmds = {
"cd c:\temp",
"@echo off &setlocal",
EvalInsert("set \!"search=%^findchar^\!""),
"set \!"replace=% \!"",
EvalInsert("set \!"textfile=^infid^\!""),
EvalInsert("set \!"newfile=^outfid^\!""),
"(for /f \!"delims=\!" %%i in (%textfile%) do (",
" set \!"line=%%i\!"",
" setlocal enabledelayedexpansion",
" set \!"line=!line:%search%=%replace%!\!"",
" echo(!line!",
" endlocal",
"))>\!"%newfile%\!""
// , "del %textfile%",
// "rename %newfile% %textfile%"
};
txt = concat items(EvalList(dos_cmds), "\!N");
f = Save Text File("c:\temp\Find_Replace.bat", txt, Mode("replace"));
Web("c:\temp\Find_Replace.bat"); //simple command to execute a .bat
dt = Open( outfid,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Spaces, Space, 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( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
If( File Exists(outfid), Delete File(outfid) );
这可以使用 JSL 以多种方式完成,但是,假设您想以交互方式执行此操作,我将这样做
- 转到 Tables==>Stack 并将列 1 - 5 堆叠到新的数据表中
- 在新表格中,右键单击缺少的单元格之一,然后选择“选择匹配单元格”。
- 删除有缺失值的行
- 创建一个新列,将其命名为“Column”,并在列信息中指定该列将数据初始化为“序列数据”,From 值为 1,To 值为 5(堆叠的列数)以及 Step 值到4(原始数据表的行数)
- 现在转到表==>拆分并选择名为“Column”的列作为拆分依据选择,并选择名为“Data”的列作为拆分列。
- 生成的新数据表应该允许您执行表==>更新或仅进行剪切和粘贴以将新结果复制回原始数据表