從網頁下載的txt文件,這個文件有數據,如何使用JSL將這樣的txt文件處理為表格數據?
我無法用 JSON 處理它。謝謝專家!

// the U.txt file has 2 extra characters at the
// start and 3 extra characters at the end.
// visual inspection is the only way I know to
// figure that out.
// using JMP 15's json wizard
x = Load Text File( "E:/U.txt" );
x = Substr( x, 3, Length( x ) - 5 );
Open( Char To Blob( x ), jsonwizard( 0 ) );
// using JMP 14's basic json support
x = Load Text File( "E:/U.txt" );
x = Substr( x, 3, Length( x ) - 5 );
Open( Char To Blob( x ), "json" );
如果您有 JMP 15,您還可以交互使用該嚮導(使用 JsonWizard(1) 查看 GUI。)它仍然需要幫助刪除多餘的字符。上面的 JSL 將 txt 加載到 x 中,使用 substr() 刪除 2 和 3 個字符,然後通過 blob 將數據傳遞給 open(),而不重新寫入磁盤。 如果重寫到磁盤,還可以將擴展名更改為 .json。
JMP 15's wizard also guesses the datatypes better in this exampleJMP 15's wizard also guesses the datatypes better in this exampleJMP 15 的嚮導還可以更好地猜測此示例中的數據類型
@大衛巴伯
不,你是對的。 我花了兩個小時根據我的方法編寫了一個腳本,並遇到了許多繁瑣的問題。 我現在沒有更多時間了。
嗨,lwx228,
下面您將找到一個腳本,它將導入數據、適當地重新編碼、將文本放入列、堆棧、拆分以獲得最終表。
一些評論,因為這是一種非常奇怪的數據格式:
- 與普通 txt 文件相比,分隔符不一致:
- 逗號用於字段中並作為分隔符,
- 也使用引號,但不是每個元素都使用引號,
- 括號有多種使用方式
- 有時缺失值會出現“null”聲明,但有時則不會!
- 第一行沒有列名稱,它們位於數據字段內。 這使得有必要提取這些並執行堆棧/拆分操作以獲取需要分析的列(可能有人有更簡單的方法 - 歡迎聽到它;) )
- 因此,默認設置將不起作用,因為您將得到每行錯誤的字段數!
- 最好的方法是讀取每行的整個字符串,然後使用重新編碼和列實用程序在 JMP 中執行其餘操作。 希望這些也可以與類似的文件一起使用。
- 要編寫這些內容的腳本,您必須測試每個步驟,如果該步驟看起來沒問題,請將腳本保存/複製到腳本窗口中並執行下一步。
- 這就是下面的腳本的創建方式。
Names Default to Here(1);
//Open your txt file using special settings, like } as end of line, uncheck column names, data starts row 1, ...
dt = Open(
"YOURPATH\U.txt", // exchange YOURPATH with your path to the file
columns( New Column( "c000001", Character, "Nominal" ) ),
Import Settings(
End Of Line( Semicolon, Other( "}" ) ),
End Of Field( 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" )
)
);
// Now you have one column with in each row all information after "name"
// Remove last two lines as they are just filled with brackets
dt << Clear Selected Row States;
m = matrix({N Rows(dt)-1, N Rows(dt)});
dt << Select Rows( m);
wait(2);
dt << Delete Rows();
// Best to use Recode and Column utility functions to seperate all information
dt << New Column("recoded","Character",Formula(Recode(
:c000001,
{Substitute( _rcNow, ",{", "" ), Substitute( _rcNow, "({[{", "" ),
Substitute( _rcNow, ":", "," ), Substitute( _rcNow, "\!",\!"", ";" ),
Substitute( _rcNow, ";;", ";null;" ),
Substitute( _rcNow, "\!"", "" ), Substitute( _rcNow, ",null,", ";null;" ),
Substitute( _rcNow, ",null", ";null" )
}
)));
// Get all text seperated by ; in seperated columns
dt << Text To Columns( delimiter( ";" ), columns( :recoded ) );
dt << Delete Columns(Column(1),Column(2));
// Now you have to stack the data appropriately
colNames = dt << Get Column Names();
dt_stack = dt << Stack(
Columns(colNames),
Source Label Column( "ColName" ),
Stacked Data Column( "Data" ),
Number of Series( 2 )
);
dt_stack << Delete Columns(1,3);
// Just to Split it again to get the columns with the correct names as well the data respectively
dt_final = dt_stack << Split(
Split By( :Data ),
Split( :Data 2 ),
Sort by Column Property
);
// Close data tables you do not need anymore
close(dt_stack, NoSave);
close(dt, NoSave);
// Finally you get an alphabetic order and all data is in character. So you now need to change this appropriately.
經歷這個過程很有趣,但需要一些時間來克服上述奇怪的數據挑戰。 然而,由於內置的腳本和公式不是太多:)
還有一點提示:要獲取重新編碼公式,請使用重新編碼並將其另存為公式列,然後查看公式編輯器即可。
最好的,
馬丁
創建一個包含必要數據列(例如“name”、“cname”等)的數據表。 該數據的開頭有無關的柵欄“({[”,結尾有“]})”。 替換“{”和“}”,以便獲得簡單的子列表列表。 然後迭代該列表並一次處理一個子列表。 這變量、值對的順序很簡單,因此您可以一次向數據表添加一行。
我現在沒有時間編寫一個簡單的腳本來演示這種方法。