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

2024-03-29 09:49:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我跑了子流程运行在一个单独的.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()方法将其放入一个更易于管理的列表中,但是它为一些结果引入了空格,例如“LTQ Orbitrap Velos”的结果,它作为3行输出。你知道吗

我希望结果是在一行,类似于命令提示符。使用.split('\n')也不能达到我想要的效果,因为它使项和结果成为一行。理想情况下,我需要一个位于顶行(或最左边的列)上的头,以及下面(或第一列右侧)的迭代列表。你知道吗

cmd prompt output

我想制作一个字典,但是条目和结果不匹配,因为两个列表的元素数不一样,所以使用zip()函数也没有帮助。请告知。谢谢。你知道吗


Tags: 文件csvpyfalsetrue列表fortime
4条回答

如果我理解正确,你可以只显示标题,然后在下一行显示结果。下面的例子应该可以做到这一点。你知道吗

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

相关问题 更多 >