我想將 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”的列作為拆分列。
- 生成的新數據表應該允許您執行表==>更新或僅進行剪切和粘貼以將新結果復制回原始數據表