2015-10-12 31 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

得到了相同的輸出,我需要轉移到其他的方式?或者有更好的方法來獲得這個。 –