cProfile将数据保存到文件会导致混乱的字符 [英] cProfile saving data to file causes jumbles of characters
问题描述
我在一个名为bot4CA.py的模块上使用cProfile,所以在控制台中键入:
I am using cProfile on a module named bot4CA.py so in the console I type:
python -m cProfile -o thing.txt bot4CA.py
模块运行并退出后,会创建一个名为thing的文件。 txt,当我打开它,有一些信息,其余的是一个混乱的字符,而不是一个整齐的组织的数据文件,这是我想要的。有没有人知道如何使用cProfile和最终得到整齐的数据表,就像在命令行正常使用它,除了文件?
下面是.txt文件中的一些数据的示例:
After the module runs and exits, it creates a file named thing.txt and when I open it, there is some information there, and the rest is a jumble of characters instead of a neatly organized file of data which is what I want. Does any one know how to use cProfile and end up with neatly organized table of data like when using it normally on command line, except in a file? Here's an example of some of the data in the .txt file:
{( s) build\bdist.win32\egg\colorama\winterm.pyi' t reset_all( i i gpàÂs% ?geOÙHÌœE?{( s- build\bdist.win32\egg\colorama\ansitowin32.pyi¥
我真正想要的是当调用cProfile.run()时会发生什么,这导致一个整齐组织的表打印显示执行
What I really want is what happens when you invoke cProfile.run() which results in a neatly organized table printed showing the execution times of all the functions except instead of printed, saved in a file as this program is fairly large and runs a lot of functions.
推荐答案
import pstats
p = pstats.Stats('thing.txt')
p.sort_stats('cumulative').print_stats(10)
这些都是在文档中,当然,在那里,即时用户手册,它解释了一切。
It's all in the documentation, of course. Go over the "instant user's manual" in there, it explains everything.
这篇关于cProfile将数据保存到文件会导致混乱的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!