5

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.

1
  • What are you expecting as output? Oct 12, 2015 at 3:56

1 Answer 1

1

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()
8
  • I think It become worse, because it also printing single line and splitted into multiple lines. Oct 12, 2015 at 5:27
  • Try the latest - worksheet.write_column('A1', map(str.strip, chain.from_iterable(DataList))) Oct 12, 2015 at 5:34
  • got same output, do I need to shift to other way? or there is a better way to get this. Oct 12, 2015 at 5:40
  • Can you show exact output you are getting right now? Oct 12, 2015 at 5:43
  • Instead of above change (same code as your previous code) , what happens if you do DataList.extend() instead of append when creating it? Oct 12, 2015 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.