Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get output line by line using xlsWriterr

I am simply printing line by line list from the loop and my output is something like this:

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

But in my excel output im just only getting the first line something like this:

enter image description here

My expected result is these:

enter image description here

My current Code is 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()

Any suggestion or comments.

like image 220
Syntax Rommel Avatar asked Nov 09 '22 03:11

Syntax Rommel


1 Answers

The issue is that you are overwriting the column with new values in each iteration. Your code should be looking something like -

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

You should keep the DataList outside the outer loop and only update the worksheet outside that loop. Example -

#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()
like image 181
Anand S Kumar Avatar answered Nov 14 '22 22:11

Anand S Kumar