Minion
Minion

Reputation: 135

Writing console output(space seperated text) to seperate columns of excel sheet using Python

I have a console output , which has multiple rows of space separated strings.for ex-

Hi everyone

Hello to all

Welcome to python

I want to write them into separate rows of excel file with each string into separate column for ex-

     A1      A2         A3
B1   Hi      everyone
B2   Hello   to         all
B3   Welcome to         python

Do we have any (text to columns) utility in xlwt package of python.?

UPDATE I have an exe which has got these print statements ,outputting all at once.I just execute that exe and get the prints on console.I want to read from the console and write to excel sheet like I have explained above.Here is the code:

#!/usr/bin/env python
import subprocess
import xlwt
process=subprocess.Popen('Test_Project.exe',stdout=subprocess.PIPE)
wb=xlwt.Workbook()
sheet=wb.add_sheet('python')
for rows in range(10):    #let say 10 rows and columns we have
    for cols in range(10):
        out = process.stdout.readline()
        sheet.write(rows,cols,out)
        print out
wb.save('stdoutput.xls')

Upvotes: 2

Views: 2013

Answers (2)

RedBaron
RedBaron

Reputation: 4755

Assuming you'll want the process to finish before you start procesing the output

#!/usr/bin/env python
import subprocess
import xlwt
process=subprocess.Popen('Test_Project.exe',stdout=subprocess.PIPE)
out,err = process.communicate()
wb=xlwt.Workbook()
sheet=wb.add_sheet('python')
row = 0
for line in out.split('\n'): 
    for i,wrd in enumerate(line.split()):    
        sheet.write(row,i,wrd)
    row=row+1 
wb.save('stdoutput.xls')

Upvotes: 1

lost
lost

Reputation: 2391

Just write it as .csv.

Assuming you've got row which is a space-separated string:

outfile.write(','.join(row.split())

Upvotes: 3

Related Questions