我正在 VOLTTRON 平台中实现 ExternalData 代理,以从 CAISO OASIS 数据库中提取 CSV 数据。OASIS 返回包含 XML 或 CSV 数据的 .zip 文件。通过修改 _handle_csv 函数以提取存档并将生成的 .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 目录中。任何建议,将不胜感激。