从子进程输出中正确拆分列表

split list properly from subprocess output

我 运行 subprocess.run 在一个单独的 .py 文件上,这给了我一个难以阅读的混乱列表。我做了一个 for 循环,它为每次迭代生成一个 csv 文件,其中一个迭代看起来像:

Version 3.1.5.0\r\nGetFileName C:\users\trinh\downloads\higgi022_test.raw\r\nGetCreatorID thermo\r\nGetVersionNumber 64\r\nGetCreationDate time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=51, tm_sec=11, tm_wday=3, tm_yday=1, tm_isdst=0)\r\nIsNewFile False\r\nIsThereMSData True\r\nHasExpMethod True\r\nInAcquisition False\r\nGetNumberOfControllers 1\r\nGetAcquisitionDate \r\nGetUniqueCompoundNames ('',)\r\nGetInstrumentDescription \r\nGetInstrumentID 0\r\nGetInstSerialNumber SN03464B\r\nGetInstName **LTQ Orbitrap Velos**\r\nGetInstModel LTQ Orbitrap Velos\r\nGetInstSoftwareVersion 2.6.0 SP3\r\nGetInstHardwareVersion \r\nGetNumInstMethods 4\r\nGetInstMethodNames ('LTQ', 'EksigentNanoLcCom_DLL', 'NanoLC-AS1 Autosampler', 'EksigentNanoLc_Channel2')\r\nGetVialNumber 0\r\nGetInjectionVolume 0.0\r\nGetInjectionAmountUnits \r\nGetSampleVolume 0.0\r\n############################################## END SECTION###################################\r\n

我尝试使用 split() 方法将其放入更易于管理的列表中,但是它为某些结果引入了白色 space,例如 'LTQ Orbitrap Velos' 的结果,它被输出作为 3 行。

我希望结果在一行,类似于cmd提示符。使用 .split('\n') 也没有达到我想要的效果,因为它使项目和结果成为一行。理想情况下,我想要一个位于顶行(或 left-most 列)的 header 和下方(或第一列右侧)的迭代列表。

我想制作一本字典,但项目和结果不匹配,因为两个列表的元素数量不同,因此使用 zip() 函数无济于事。请指教。谢谢

如果我没理解错的话,你可以只显示 header,然后在下一行显示结果。下面应该用你的例子来做。

def cleanup(rslts):
    # looking at the following line, working from inside outward:
    # first split rslts on new lines
    # then loop over it (`for r in rslts.split...`)
    # but only accept lines which are not empty (the `if r` clause)
    # now, we just loop over each line from that generator 
    # expression - the `for aline in (...)` part
    for aline in (r for r in rslts.split('\r\n') if r):
        # treat the `END SECTION` differently - just print it
        if aline.startswith('###'):
            print(aline)
            continue  # goes back to the `for line in (...)`

        # `aline.split(' ', 1) splits on spaces, but a maximum of 1 time
        # now assign `header` the first thing on the left of the `=`
        # and `footer` the next item
        header, remainder = aline.split(' ', 1)  
        print(header)
        print(remainder)

if __name__ == '__main__':
    # messy results below:
    rslts = """Version 3.1.5.0\r\nGetFileName C:\users\trinh\downloads\higgi022_test.raw\r\nGetCreatorID thermo\r\nGetVersionNumber 64\r\nGetCreationDate time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=51, tm_sec=11, tm_wday=3, tm_yday=1, tm_isdst=0)\r\nIsNewFile False\r\nIsThereMSData True\r\nHasExpMethod True\r\nInAcquisition False\r\nGetNumberOfControllers 1\r\nGetAcquisitionDate \r\nGetUniqueCompoundNames ('',)\r\nGetInstrumentDescription \r\nGetInstrumentID 0\r\nGetInstSerialNumber SN03464B\r\nGetInstName **LTQ Orbitrap Velos**\r\nGetInstModel LTQ Orbitrap Velos\r\nGetInstSoftwareVersion 2.6.0 SP3\r\nGetInstHardwareVersion \r\nGetNumInstMethods 4\r\nGetInstMethodNames ('LTQ', 'EksigentNanoLcCom_DLL', 'NanoLC-AS1 Autosampler', 'EksigentNanoLc_Channel2')\r\nGetVialNumber 0\r\nGetInjectionVolume 0.0\r\nGetInjectionAmountUnits \r\nGetSampleVolume 0.0\r\n############################################## END SECTION###################################\r\n"""
    cleanup(rslts)  # pass messy results into a function to pretty up output