2015-10-12 28 views
5

我只是打印由线列表行从环路获得输出线和我的输出是这样的:如何通过线使用xlsWriterr

[' 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输出IM仅仅只获得了第一线是这样的:

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

任何建议或评论。

+0

什么是你期待的输出? –

回答

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

你应该保持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() 
+0

我觉得它变得更糟,因为它也印一行,并分裂成多个行。 –

+0

试用最新 - 'worksheet.write_column( 'A1',地图(str.strip,chain.from_iterable(DataList控件)))' –

+0

得到了相同的输出,我需要转移到其他的方式?或者有更好的方法来获得这个。 –