如何使用xlsWri逐行获取输出

2024-05-12 23:45:32 发布

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

我只是从循环中逐行打印列表,输出如下:

['    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']

但在我的excel输出中,我只得到了第一行这样的内容:

enter image description here

我的预期结果是:

enter image description here

我现在的代码是:

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()

任何建议或意见。你知道吗


Tags: 代码in内容列表forifcountline
1条回答
网友
1楼 · 发布于 2024-05-12 23:45:32

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

#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()

您应该将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()

相关问题 更多 >