首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python统计信息:如何将其写入(人类可读的)文件

Python统计信息:如何将其写入(人类可读的)文件
EN

Stack Overflow用户
提问于 2012-11-24 00:09:42
回答 5查看 13.7K关注 0票数 24

我使用的是Python的hotshot分析器:http://docs.python.org/2/library/hotshot.html

它显示了如何打印统计数据:

代码语言:javascript
复制
stats.print_stats(20)

但是我如何将其放入文件中呢?我不确定如何获取信息,所以我可以使用write()将其写入文件。

编辑:

我希望以这种方式打印出来的结果和打印出来的结果一样容易阅读:

代码语言:javascript
复制
stats = hotshot.stats.load("stones.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20) 

所以看起来是这样的:

代码语言:javascript
复制
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    3.295    3.295   10.090   10.090 pystone.py:79(Proc0)

(因此,当我打开stones.prof时,它看起来并不像这样)

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-11-24 17:27:03

我最终重写了print_stats()函数,从从pstats.py复制开始。它返回一个字符串,然后可以将该字符串写入文件。我没有测试每个if-else循环,只是在我需要它的示例中测试了它。我把原始行注释掉了。我保留了变量名不变,尽管函数不再使用真正的"self“。

代码语言:javascript
复制
stats = hotshot.stats.load("stones.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
readable_str = xprint_stats(stats, 20)

import pstats
def xprint_stats(self, *amount):
    x = ""
    for filename in self.files:
        x += " " + filename
    #if self.files: print >> self.stream
    # ?
    indent = ' ' * 8
    for func in self.top_level:
        #print >> self.stream, indent, xfunc_get_function_name(func)
        x += indent + pstats.func_get_function_name(func)

    #print >> self.stream, indent, self.total_calls, "function calls",
    x +=  indent + str(self.total_calls) + " function calls" + " "
    if self.total_calls != self.prim_calls:
        #print >> self.stream, "(%d primitive calls)" % self.prim_calls,
        x += "(%d primitive calls)" % self.prim_calls + " "
    #print >> self.stream, "in %.3f seconds" % self.total_tt
    #print >> self.stream
    x +=  "in %.3f seconds" % self.total_tt + "\n"
    #width, list = stats.get_print_list(amount)
    msg, width, list = xget_print_list(stats, amount)
    x += msg

    if list:
        #self.print_title()
        x += "\n" + '   ncalls  tottime  percall  cumtime  percall filename:lineno(function)'
        x += "\n"
        for func in list:
            #self.print_line(func)
            x +=  xprint_line(self, func) + "\n"
#        print >> self.stream
#        print >> self.stream
    #return self
    return x

def xprint_line(self, func):  
    x = ""
    cc, nc, tt, ct, callers = self.stats[func]
    c = str(nc)
    if nc != cc:
        c = c + '/' + str(cc)
#    print >> self.stream, c.rjust(9),
#    print >> self.stream, f8(tt),
    x +=  c.rjust(9) + " "
    x +=  pstats.f8(tt) + " "
    if nc == 0:
        #print >> self.stream, ' '*8,
       x +=  ' '*8 
    else:
        #print >> self.stream, f8(float(tt)/nc),
        x +=  pstats.f8(float(tt)/nc) + " "
    #print >> self.stream, f8(ct),
    x +=  pstats.f8(ct) + " "
    if cc == 0:
        #print >> self.stream, ' '*8,
        x +=  ' '*8
    else:
        #print >> self.stream, f8(float(ct)/cc),
        x +=   pstats.f8(float(ct)/cc) + " "
    #print >> self.stream, func_std_string(func)
    x +=  pstats.func_std_string(func) + " "
    return x

def xget_print_list(self, sel_list):
    width = self.max_name_len
    if self.fcn_list:
        stat_list = self.fcn_list[:]
        msg = "   Ordered by: " + self.sort_type + '\n'
    else:
        stat_list = self.stats.keys()
        msg = "   Random listing order was used\n"

    for selection in sel_list:
        stat_list, msg = self.eval_print_amount(selection, stat_list, msg)

    count = len(stat_list)

    if not stat_list:
        return 0, stat_list
    #print >> self.stream, msg
    if count < len(self.stats):
        width = 0
        for func in stat_list:
            if  len(pstats.func_std_string(func)) > width:
                width = len(pstats.func_std_string(func))
    #return width+2, stat_list
    return msg, width+2, stat_list
票数 2
EN

Stack Overflow用户

发布于 2013-05-09 01:34:25

Stats接受一个可选的'stream‘参数。只需打开一个文件并将打开的文件对象传递给Stats构造函数,如下所示。从那时起,对print_stats()的任何调用都将输出到您传递给构造函数的流。希望这能有所帮助。:)

代码语言:javascript
复制
with open('path/to/output', 'w') as stream:
    stats = pstats.Stats('path/to/input', stream=stream)
    stats.print_stats()
票数 44
EN

Stack Overflow用户

发布于 2013-04-14 03:25:12

输出重定向怎么样?

代码语言:javascript
复制
import sys
import pstats
sys.stdout = open('readable.profile', 'w')
p = pstats.Stats('input.profile')
p.print_stats()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13532531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档