Web API打開一個zip文件?大家好,
我正在與客戶一起從Web API獲取數據,該數據恰好以zip格式向用戶發送數據。 我已經設法弄清楚如何將存檔中的默認值更改為CSV,但我仍然遇到了將數據拉入JMP而不是下載文件夾的挑戰。
數據來自:
web命令將很好地下載數據。 但是,如果數據從Web API進入JMP,那麼它對客戶才真正有用。 @Craige_Hales 有一些很棒的帖子可以讓我進一步了解Run Program()和cURL路徑:
但是我還是卡住了。 也許我需要了解有關Run Program讀取語句的更多信息,或者更多地了解cURL選項。
/* generating a start time string for query injection */
starttime = "20160418T07:00-0000";
startdatetime = "&startdatetime=" || starttime;
If( starttime == "",startdatetime = "");
/* generating a end time string for query injection */
endtime = "";
enddatetime = "&enddatetime=" || endtime;
If( endtime == "",enddatetime = "");
/* FILENAME */
FILENAME = "CURR_LMP_GRP";
// CAISO (california energy pricing API)
oasis = "http://oasis.caiso.com/oasisapi/";
query = "GroupZip?groupid="|| FILENAME || startdatetime || "&version=1&resultformat=6";
url = oasis || query;
// download the data
web(url);
// the goal is to directly pull the item into JMP
/* RunProgram to pull the table into JMP (taken from a Craig Hales Post) */
If( HostIs("Windows"),
data = RunProgram(
executable( Convert File Path( "$desktop", windows ) || "curl.exe" ),
options( "-k -s --basic " || url ),
readfunction( "blob" )
);
, // else mac version
data = RunProgram(
executable( "/usr/bin/curl" ),
options( {"-k", "-s", "--basic", url} )
, readfunction( "blob" )
);
);
try( dt = open(data, "text"); error="", error=exception_msg; dt=empty() );
show(dt,error);
對我們來說,這個問題還有一些潛在的可擴展性。 想想能夠抓住亞馬遜的web api進行客戶評論的演示能力。 運行Program()和a python腳本可以做到這一點。
--Nate
Re:Web API打開一個zip文件?實際上,在我擁有JMP文件之前避免寫入磁盤確實是我的意圖。感謝您的見解!
Re:Web API打開一個zip文件?要打開的第一個arg通常是一個字符串...文件的* name *。當它是blob時,open將其視為文件的* contents *,但沒有擴展名(.jmp等),因此需要打開第二個arg(“data”,“script”等等) )。(在內部,open可以將blob寫入臨時位置,具體取決於打開的子系統。源腳本打開(“/ 000000002A75C3D0 /”... 告訴我它沒有寫入磁盤,這是你所希望的。)Re:Web API打開一個zip文件?再次感謝Craige!
下面是一個完整的JSL示例,其中包含已實現的建議項目。
/* generating a start time string for query injection */
starttime = "20160418T07:00-0000";
startdatetime = "&startdatetime=" || starttime;
If( starttime == "",startdatetime = "");
/* generating a end time string for query injection */
endtime = "";
enddatetime = "&enddatetime=" || endtime;
If( endtime == "",enddatetime = "");
/* FILENAME */
FILENAME = "CURR_LMP_GRP";
// CAISO (california energy pricing API)
oasis = "http://oasis.caiso.com/oasisapi/";
query = "GroupZip?groupid="|| FILENAME || startdatetime || "&version=1&resultformat=6";
url = oasis || query;
//open the zip download which may have multiple files
za = open( url, "zip"); // 2nd zip important!
zaList = za << dir; //get a list of the files in the archive
for( i = n items(zaList), i > 0, i--,
blob = za<<READ></READ>
令人著迷的是指定格式(blob)使open命令工作,但格式(文本)不會。
Re:Web API打開一個zip文件?try(deletefile("$temp/deleteme.zip"));
za = open("$temp/deleteme.zip","zip");
member = za<<WRITE></WRITE>
用blob打開。 如果你沒有blob,請使用charToBlob()。
Re:Web API打開一個zip文件?以下將創建一個用於打開的文本文件,但我不禁覺得應該有更好的方法。
Save Text File("$TEMP/" || FILENAME || Char(i) || ".txt",txt);
open("$TEMP/" || FILENAME || Char(i) || ".txt");
--Nate
Re:Web API打開一個zip文件?謝謝克雷格!
這讓我幾乎到處都是。 我可以發誓我在開放語句中嘗試使用zip作為參數而沒有運氣(可能是不正確的情況或字符串vs不),但是使用您的示例代碼我已經能夠獲得實際讀入的數據!
//open the zip download
za = open( url, "zip"); // 2nd zip important!
zaList = za << dir; //get a list of the files in the archive
for( i = n items(zaList), i > 0, i--,
txt = za<<READ></READ>
我的下一步是將txt轉換為表格。
Re:Web API打開一個zip文件?嘗試這樣的事情:
za = open(“ http://someurl.that.downloads.a.zip “,”zip“); //第二個拉鍊重要!
節目(ZA <<成員);
txt = za << read(“member1”); //上面列表中的名稱
za是腳本索引中的ZipArchive對象。網址可能不會以.zip結尾,但必須下載一個zip。