如何從匯入的 json 檔案/jmp 關聯數組設定變數?大家好,
我正在使用 Application Builder 開發一個與 Python 整合進行資料分析的工具。 我創建了一個按鈕來匯出使用者的參數選擇,以防他們想要稍後重新上傳這些參數並執行相同的資料預處理。 目前,我匯出參數的方式是先將它們編寫為 csv(使用 Save Text File(...) ),然後可以將其作為 jmp 表打開,從那裡,兩列(變數和值)可以設定為關聯數組,我還將其保存為JSON 檔案以在後端應用程式中使用。 我已經包含了導出的 jmp 表可能的樣子的圖片(對於變量 9 和 10,根據選擇,可能有一個、兩個或三個附加變量,因此該表(和關聯數組/json)不是尺寸始終相同。 有些值是字串,有些是數字。
我的問題是如何最好地重新上傳這些參數(最好是從 jmp 表和 JSON 檔案),以便變數正確地設定為其值並可以在以後的計算中使用。 我一直在使用關聯數組中的 For Each(...) ,但無法正確設定變數。 這不是保存參數的最直接的方法,但我無法弄清楚如何將變數和值正確編寫為直接發送到 jmp 表。 我目前用來保存參數的方法適合我的目的,但如果其他方法或更改 csv/表的結構/佈局將使參數的上傳更容易,我願意接受建議。
謝謝你的幫忙。
資料存取回覆:如何從匯入的 json 檔案/jmp 關聯數組設定變數?再次感謝, @Craige_Hales ! 這是一篇很有幫助的文章。 儘管它可能有點笨重,但我應該能夠列出所有按鍵組合併使用這些方法使其正常工作。
回覆:如何從匯入的 json 檔案/jmp 關聯數組設定變數?您也許能夠使用關聯數組的預設值,或者您可以在使用之前明確測試該鍵是否存在。
看@jthi中的評論關聯數組:Try()、Contains() 還是預設值?。這組按鍵是否夠小,可以列出所有可能的組合? 或者它是一個無法預測的集合,您需要從中建立新變數?
回覆:如何從匯入的 json 檔案/jmp 關聯數組設定變數?謝謝回覆! 我認為關聯數組查找非常簡單,我只是沒有嘗試正確的語法。
我仍然有一個問題,但是...... 對於我的兩個變量,有幾個不同的選項(變量1 可以命名為“var1option1”、“var1option2”等),並且所有不同的選項都可能有一個、兩個或三個特定於該變量的附加變數。選項選擇。 所有這些變數名稱都是不同的。 有沒有辦法在事先不知道選擇是什麼的情況下卸載這些參數?
就像在這種情況下一樣,要么存在“var1option1”,要么存在“var1option2”......不能同時存在,但我無法事先知道,並且該腳本必須適用於所有可能的情況。 當變數名不存在於關聯數組中時,有沒有辦法避免錯誤? 看來我必須使用 If 語句?
var1option1 = restoredVariables["var1option1"];
var1option1sub1 = restoredVariables["var1option1sub1"];
var1option1sub2 = restoredVariables["var1option1sub2"];
var1option1sub3 = restoredVariables["var1option1sub3"];
var1option2 = restoredVariables["var1option2"];
var1option2sub1 = restoredVariables["var1option2sub1"];
var1option2sub2 = restoredVariables["var1option2sub2"];
再次感謝!
回覆:如何從匯入的 json 檔案/jmp 關聯數組設定變數?盡量避免在磁碟上保留相同資料的多種表示形式。 由於您已經混合使用了 Python 和 JSON,因此我建議使用 JSON 作為磁碟格式。 這裡有兩個例子;第一個假設您喜歡 JMP 中的關聯數組,第二個假設您使用資料表。 我傾向於第一個,因為名字效果更好(如下。)
// 1st example using Associative Array
variables = ["nThings" => 3,
"type" => "favs",
"things" => {"raindrops", "whiskers", "kettles"}];
filename = Save Text File( "$temp/variables.json", As JSON Expr( variables ) );
restoredVariables = Parse JSON( Load Text File( filename ) );
// this is not needed unless you want to compare the restored data to the original...
If( variables == restoredVariables,
Write( "\!nGood restore\!n" ),
Write( "\!nRestore does not match\!n" )
);
Show( variables, restoredVariables );
// 2nd example, using data table
dt = New Table( "variables",
New Column( "key", "character", values( variables << getkeys ) ),
New Column( "val", "expression", values( variables << getvalues ) )
);
dt << save( "$temp/table.json" );
dtRestored = Open( "$temp/table.json" );
dtRestored:val << datatype( "expression" );
// this is not needed, unless you want to compare the restored table to the original...
ok = (dt << Compare Data Tables( showwindow( 0 ), Unlink All, Link( {"key", "key"} ), Link( {"val", "val"} ) )) << Compare with( dtRestored );
Write( "\!n\!nData table on disk:\!n", Load Text File( "$temp/table.json" ) );
If( ok,
Write( "\!ntables match" ),
Write( "\!nhmmm, tables are different" )
);
使用關聯數組,查找很簡單:
type = restoredVariables["type"];
“最愛”
回覆:如何從匯入的 json 檔案/jmp 關聯數組設定變數?這是一個非常簡單的範例來說明我正在嘗試執行的操作(對於關聯數組情況,而不是 .json 檔案情況):
AA = Associative Array();
AA["One"] = {1, 2, 3};
AA["Two"] = {"a", "b", "c"};
key_list = AA << Get Keys();
val_list = AA << Get Values();
For(i=1, i<=N Items(key_list), i++,
key_list[i] = val_list[i];
Show(key_list[i]);
);
其日誌輸出為:
// key_list[i] = {1, 2, 3};
// key_list[i] = {"a", "b", "c"};我想要的結果是(因此可以使用 Python Send(...) 將這些變數傳送到 Python 並在那裡使用):
// One = {1, 2, 3};
// Two = {"a", "b", "c"};我覺得這非常接近我想要的工作方式,但我沒有這樣的運氣!
謝謝!