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

时间:2016-12-04 03:23:08

标签: python list csv split export-to-csv

我在一个单独的.py文件上运行了subprocess.run,这给我一个很难读的杂乱列表。我创建了一个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()方法将其放入一个更易于管理的列表中,但是它为某些结果引入了空格,例如LTQ Orbitrap Velos'的结果,它输出为3行。

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

cmd prompt output

我想创建一个字典,但是项目和结果不匹配,因为这两个列表不会有相同数量的元素,因此使用zip()函数无济于事。请指教。感谢。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你可以只显示标题,然后是下一行的结果。以下内容应与您的示例相同。

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