如何使用xlsWriterr逐行获取输出

Roc*_*oll 5 python xlsxwriter

我只是从循环中逐行打印,我的输出是这样的:

['    4.0\n', '   17.2\n', '    7.0\n']   
['    0.0\n']
['    4.0\n', '   16.7\n', '    4.0\n']
['    4.0\n', '   16.7\n', '    4.0\n']
['    4.0\n', '   16.7\n', '    4.0\n']
['    4.0\n', '   16.7\n', '    4.0\n']
['    4.0\n', '   16.4\n', '    4.0\n']
Run Code Online (Sandbox Code Playgroud)

但在我的excel输出中我只是得到第一行这样的东西:

在此输入图像描述

我的预期结果是:

在此输入图像描述

我目前的代码在这里:

count = 0   
DataList = []                                               
for line, file in enumerate(PM2Line):       
    if POA in file: 
        DataList.append(file[32:50])                                
print DataList #--> this will print the list of output      
worksheet.write_column('A1', DataList) #--> My problem is just getting first line.  
workbook.close()
Run Code Online (Sandbox Code Playgroud)

任何建议或意见.

Ana*_*mar 1

问题是您在每次迭代中都用新值覆盖该列。你的代码应该看起来像 -

#some loop
    count = 0   
    DataList = []                                               
    for line, file in enumerate(PM2Line):       
        if POA in file: 
            DataList.append(file[32:50])                                
    print DataList #--> this will print the list of output      
    worksheet.write_column('A1', DataList) #--> My problem is just getting first line.  
    workbook.close()
Run Code Online (Sandbox Code Playgroud)

您应该将DataList外部保留在外循环之外,并且只更新该循环之外的工作表。例子 -

#open worksheet here instead of inside the loop.
DataList = []
#some loop
    count = 0                                              
    for line, file in enumerate(PM2Line):       
        if POA in file: 
            DataList.append(file[32:50])                                
    print DataList     
worksheet.write_column('A1', DataList)
workbook.close()
Run Code Online (Sandbox Code Playgroud)