问题描述
如何调用将 CSV 输出到 STDOUT 的子进程,并将其读入 dict?
我有一系列脚本来查询各种管理服务器,并将它们的数据库作为 CSV 转储到 STDOUT。在单独的脚本中,我想调用它们,并将它们的输出捕获到字典中。
这个伪代码展示了这个概念:
NetbrainOutput = subprocess.run(["python3","../Python:Netbrain/netbrain.py"],capture_output=True)
ZenossOutput = subprocess.run(["python3","../Python:ZenOSS/zenoss.py"],capture_output=True)
NetbrainCSV = csv.reader(NetbrainOutput)
ZenossCSV = csv.reader(ZenossOutput)
实际上,我想要做的是调用子进程,并将该子进程的输出连接到一个管道,该管道的行为就像一个我可以读取的文件。我想知道如何将 CSV 转换为 dict,我不知道如何将 CSV 模块连接到子流程输出。或者,也许我的做法是错误的?
解决方法
我想通了。
import sys,subprocess,csv,io
# I developed this on python 3.6
# subprocess module had a fair number of changes around this time
assert sys.version_info >= (3,6)
script = "ScriptThatOutputsCSV.py"
subprocessPipe = subprocess.Popen(["python3",script],stdout=subprocess.PIPE)
# unfortunately Popen opens the pipe as a binary stream
# DictReader requires a text stream :(
StringIO = io.TextIOWrapper(subprocessPipe.stdout)
reader = csv.DictReader(StringIO)
# unfortunately,list(reader) creates a list of DictReader objects
# we want a list of dicts,so,have to do it the hard way :(
ListedDict = []
for row in reader:
newDict = {}
for key in reader.fieldnames:
newDict[key] = row[key]
ListedDict.append(newDict)
# ListedDict is now a list of dictionaries
# correlating to the CSV that script sent to STDOUT