【问题标题】:I am implementing the ExternalData agent in VOLTTRON to get data from CAISO OASIS我正在 VOLTTRON 中实现 ExternalData 代理以从 CAISO OASIS 获取数据
【发布时间】:2017-03-22 18:12:34
【问题描述】:

我正在 VOLTTRON 平台中实现 ExternalData 代理,以从 CAISO OASIS 数据库中提取 CSV 数据。 OASIS 返回包含 XML 或 CSV 数据的 .zip 文件。在处理 csv 数据之前,我可以通过修改 _handle_csv 函数来提取存档并将生成的 .csv 打开到文件对象中,从而粗略地获得我想要的 CSV 数据:

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 目录中。任何建议将不胜感激。

【问题讨论】:

    标签: csv zipfile volttron


    【解决方案1】:

    使用 ZipFile.open 方法。 Docs

    然后您可以将生成的文件(如对象)传递给 CSV 解析器。

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

    【讨论】:

    • 谢谢凯尔,我一定在 python 文档中跳过了这个。我会试试的。
    • 不客气。这可能是添加到代理的好功能。您是否介意在此处创建一个要求此功能的问题:github.com/VOLTTRON/volttron/issues
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    相关资源
    最近更新 更多