cProfile 将数据保存到文件会导致字符混乱
我在名为 bot4CA.py 的模块上使用 cProfile,因此在控制台中输入:
python -m cProfile -o thing.txt bot4CA.py
模块运行并退出后,它会创建一个名为 thing.txt 的文件,当我打开它时,其中有一些信息,其余的是混乱的字符而不是我想要的整齐组织的数据文件。有谁知道如何使用 cProfile 并最终得到组织整齐的数据表,就像在命令行上正常使用它一样,除了在文件中? 下面是 .txt 文件中的一些数据的示例:
{( 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() 时会发生什么,这会导致打印出一个组织整齐的表格,显示所有函数的执行时间(除了打印之外),保存在一个文件中,因为该程序相当大并且运行很多功能。
I am using cProfile on a module named bot4CA.py so in the console I type:
python -m cProfile -o thing.txt bot4CA.py
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¥
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
pstats
模块来解析此文件并从中提取用户友好格式的信息。例如:当然,这些都在文档中。查看那里的“即时用户手册”,它解释了一切。
You should use the
pstats
module to parse this file and extract information in user-friendly format from it. For example:It's all in the documentation, of course. Go over the "instant user's manual" in there, it explains everything.
直接转储统计信息:
pstats 中还有一个 shell
,我们可以发出
stats
或sort
命令,如下所示:我在这里喜欢的一个微观功能是我可以反转排序原生顺序<3
to dump the stats driectly:
pstats also has a shell
within we can issue
stats
orsort
commands like so:a micro-feature I enjoyed here is I get to reverse the sort order natively <3
其他答案更强大、更灵活,但如果您只想快速输出,请使用
>
而不是-o
。这将以纯文本格式保存报告。The other answers are more powerful and flexible, but if you just want to get a quick output, use
>
instead of-o
. That will save the report in plain text format.