2017-03-22 24 views
0

我正在VOLTTRON平臺中實施ExternalData代理以從CAISO OASIS數據庫中提取CSV數據。 OASIS返回包含XML或CSV數據的.zip文件。我能粗製濫造前處理CSV數據得到我想要通過修改_handle_csv函數提取存檔,並打開生成的.csv到一個文件對象的CSV數據:我正在實施VOLTTRON中的ExternalData代理以從CAISO OASIS獲取數據

def _handle_csv(self, headers, request, url, source_topic, source_params): 
    key_column = source_params.get("key", "") 
    flatten = source_params.get("flatten", False) 
    parse_columns = source_params.get("parse", []) 

    # I am expecting a zip archive containing a csv file 
    # so this is a workaround to read the zipfile and extract the csv 
    # by creating a file object with open() after extracting 
    # TODO find another way to get data w/o opening file so we dont 
    # have to do housekeeping 
    z = zipfile.ZipFile(StringIO(request.content)) 
    fname = z.namelist() 
    z.extractall() 
    file_obj = open(fname[0], 'r') 

    # orig. file object assignment assuming request already in csv format 
    # file_obj = StringIO(request.content) 

我想找到一個更好的方法來做到這一點,並避免必須執行多餘的內務管理,例如關閉.csv並將其刪除,以便文件不會堆積在agent-data目錄中。任何建議,將不勝感激。

回答

0

使用ZipFile.open方法。 Docs

然後,您可以將生成的文件(如對象)傳遞給CSV解析器。

file_obj = z.open(fname[0]) #Remove the extractall() call and this should do the trick. 
+0

謝謝凱爾,我一定跳過了python文檔。我會嘗試。 – jmattfeld

+0

不客氣。這可能是添加到代理的好功能。你介意創建一個問題在這裏要求這個功能:https://github.com/VOLTTRON/volttron/issues –

相關問題