python将输出的数据写入excel文件_Python,将打印输出附加到excel文件

I was hoping someone may be able to point me in the right direction, or give an example on how I can put the following script output into an Excel spreadsheet using xlwt. My script prints out the following text on screen as required, however I was hoping to put this output into an Excel into two columns of time and value. Here's the printed output..

07:16:33.354 1

07:16:33.359 1

07:16:33.364 1

07:16:33.368 1

My script so far is below.

import re

f = open("C:\Results\16.txt", "r")

searchlines = f.readlines()

searchstrings = ['Indicator']

timestampline = None

timestamp = None

f.close()

a = 0

tot = 0

while a

for i, line in enumerate(searchlines):

for word in searchstrings:

if word in line:

timestampline = searchlines[i-33]

for l in searchlines[i:i+1]: #print timestampline,l,

#print

for i in line:

str = timestampline

match = re.search(r'\d{2}:\d{2}:\d{2}.\d{3}', str)

if match:

value = line.split()

print '\t',match.group(),'\t',value[5],

print

print

tot = tot+1

break

print 'total count for', '"',searchstrings[a],'"', 'is', tot

tot = 0

a = a+1

I have had a few goes using xlwt or CSV writer, but each time i hit a wall and revert bact to my above script and try again. I am hoping to print match.group() and value[5] into two different columns on an Excel worksheet.

Thanks for your time...

MikG

解决方案

What kind of problems do you have with xlwt? Personally, I find it very easy to use, remembering basic workflow:

import xlwt

create your spreadsheet using eg.

my_xls=xlwt.Workbook(encoding=your_char_encoding),

which returns you spreadsheet handle to use for adding sheets and saving whole file

add a sheet to created spreadsheet with eg.

my_sheet=my_xls.add_sheet("sheet name")

now, having sheet object, you can write on it's cells using sheet_name.write(row,column, value):

my_sheet.write(0,0,"First column title")

my sheet.write(0,1,"Second column title")

Save whole thing using spreadsheet.save('file_name.xls')

my_xls.save("results.txt")

It's a simplest of working examples; your code should of course use sheet.write(row,column,value) within loop printing data, let it be eg.:

import re

import xlwt

f = open("C:\Results\VAMOS_RxQual_Build_Update_Fri_04-11.08-16.txt", "r")

searchlines = f.readlines()

searchstrings = ['TSC Set 2 Indicator']

timestampline = None

timestamp = None

f.close()

a = 0

tot = 0

my_xls=xlwt.Workbook(encoding="utf-8") # begin your whole mighty xls thing

my_sheet=my_xls.add_sheet("Results") # add a sheet to it

row_num=0 # let it be current row number

my_sheet.write(row_num,0,"match.group()") # here go column headers,

my_sheet.write(row_num,1,"value[5]") # change it to your needs

row_num+=1 # let's change to next row

while a

for i, line in enumerate(searchlines):

for word in searchstrings:

if word in line:

timestampline = searchlines[i-33]

for l in searchlines[i:i+1]: #print timestampline,l,

#print

for i in line:

str = timestampline

match = re.search(r'\d{2}:\d{2}:\d{2}.\d{3}', str)

if match:

value = line.split()

print '\t',match.group(),'\t',value[5],

# here goes cell writing:

my_sheet.write(row_num,0,match.group())

my_sheet.write(row_num,1,value[5])

row_num+=1

# and that's it...

print

print

tot = tot+1

break

print 'total count for', '"',searchstrings[a],'"', 'is', tot

tot = 0

a = a+1

# don't forget to save your file!

my_xls.save("results.xls")

A catch:

native date/time data writing to xls was a nightmare to me, as excel

internally doesn't store date/time data (nor I couldn't figure it

out),

be careful about data types you're writing into cells. For simple reporting at the begining it's enough to pass everything as a string,

later you should find xlwt documentation quite useful.

Happy XLWTing!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值