Python,将打印输出附加到excel文件 [英] Python, appending printed output to excel file
问题描述
我希望有人能够指出正确的方向,或举例说明如何使用xlwt将以下脚本输出放到Excel电子表格中。我的脚本根据需要在屏幕上打印出以下文本,但是我希望将这个输出放入一个Excel中成两列时间和值。这是打印的输出。
07:16:33.354 1
07:16:33.359 1
07:16:33.364 1
07:16:33.368 1
我的脚本到此为止。
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< len(searchstrings):
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)
如果匹配:
value = line.split()
print'\t ',match.group(),'\t',value [5],
print
print
tot = tot + 1
break
$ b b print'total count for','',searchstrings [a],'','is',tot
tot = 0
a = a + 1
我有几个使用xlwt或CSV写入器,但每次我打墙,恢复bact到我的上述脚本,然后重试。我希望在Excel工作表上将match.group()和value [5]打印到两个不同的列中。
感谢您的时间...
MikG
xlwt有什么问题?个人来说,我觉得很容易使用,记住基本的工作流程:
-
import xlwt
-
使用以下方式建立试算表:
my_xls = xlwt.Workbook(encoding = your_char_encoding)
,
返回您用于添加工作表和保存整个文件的电子表格句柄
-
使用eg添加工作表以创建电子表格。
my_sheet = my_xls.add_sheet(工作表名称)
-
现在,使用sheet对象,您可以使用sheet_name.write(row,column,value)在其单元格上写入:
my_sheet.write(0,0,First column title)
my sheet.write(0,1,Second column title) -
使用spreadsheet.save('file_name.xls')保存整个内容
my_xls.save results.txt)
这是一个最简单的工作示例;你的代码当然应该在循环打印数据中使用sheet.write(row,column,value),让它是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)#开始你的整个威武xls事情
my_sheet = my_xls.add_sheet(Results)#添加工作表it
row_num = 0#让它成为当前行号
my_sheet.write(row_num,0,match.group())#here go column headers,
my_sheet .write(row_num,1,value [5])#根据你的需要改变它
row_num + = 1#让我们改变到下一行
while a< len(searchstrings):
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 \\ d {2}:\d {2}:\d {2} .\d {3}',str)
如果匹配:
value = line.split $ b print'\t',match.group(),'\t',value [5],
#这里是单元格写入:
my_sheet.write(row_num,0, group())
my_sheet.write(row_num,1,value [5])
row_num + = 1
#就是这样...
print
print
tot = tot + 1
break
print'total count for','',searchstrings [a],''','is',tot
tot = 0
a = a + 1
#不要忘记保存文件!
my_xls.save(results.xls)
- 本地日期/时间写入xls的数据对我来说是一个噩梦,因为excel
内部不存储日期/时间数据无法计算
), - 请注意您在单元格中写入的数据类型。对于简单的报告,在开始时它足以传递所有的字符串,
以后你应该找到xlwt文档很有用。
快乐XLWTing!
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<len(searchstrings):
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<len(searchstrings):
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!
这篇关于Python,将打印输出附加到excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!