简体   繁体   English

使用Python编写控制台输出(用空格分隔的文本)以分隔Excel工作表的列

[英]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 欢迎使用python

I want to write them into separate rows of excel file with each string into separate column for ex- 我想将它们写入Excel文件的单独行中,并将每个字符串分别写入单独的列中,例如

     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.? 在python的xlwt包中是否有任何(文本转换为列)实用程序?

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: 更新我有一个exe文件,它具有这些打印语句,一次输出所有。我只是执行该exe文件并在控制台上获取打印内容。我想从控制台读取并像上面已经解释的那样写入excel表格。码:

#!/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')

Just write it as .csv. 只需将其写为.csv。

Assuming you've got row which is a space-separated string: 假设您获得的row是一个用空格分隔的字符串:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM