[腾讯课堂 | Python网络爬虫与文本分析(现在只需198元)~~ ](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650076662&idx=2&sn=95c13e697ecc4135a9cf4d494997717f&chksm=f1f744e9c680cdff1880531dffd424262cc01d738a38887d1a2d85158155b3bb4511bd237bd2&scene=21#wechat_redirect)
Hello 大家好,我是小F~
相信大部分人学习Python,肯定会用print()这个内置函数,来调试代码的。
那么在一个大型的项目中,如果你也是使用print来调试你的Python代码,你就会发现你的终端有多个输出。
那么你便不得不去分辨,每一行的输出是哪些代码的运行结果。
举个例子,运行下面这个程序。
num1 = 30
num2 = 40
print(num1)
print(num2)
输出结果。
30
40
这些输出中哪一个是num1?哪一个又是num2呢?
找出两个输出可能不是很困难,但是如果有五个以上的不同输出呢?尝试查找与输出相关的代码可能会很耗时。
当然你可以在打印语句中添加文本,使其更容易理解:
num1 = 30
num2 = 40
print("num1" num1)
print("num2" num1)
输出结果。
num1 30
num2 40
这个结果就很容易理解了,但是需要时间去写相关的信息。
这时就该「Icecream」上场了~
01. 什么是Icecream?
Icecream是一个Python第三方库,可通过最少的代码使打印调试更清晰明了。
使用pip安装Icecream库。
pip install icecream
下面,让我们通过打印Python函数的输出来进行尝试。
from icecream import ic
def plus_five(num):
return num + 5
ic(plus_five(4))
ic(plus_five(5))
输出结果如下。
ic| plus_five(4): 9
ic| plus_five(5): 10
通过使用icecream,我们不仅可以看到函数输出,还可以看到函数及其参数!
02. 检查执行情况
如果你想要找到执行代码的位置,可以通过执行如下所示的操作,来查找执行了哪个语句。
def hello(user:bool):
if user:
print("I'm user")
else:
print("I'm not user")
hello(user=True)
输出结果。
I'm user
使用icecream则无需多余的文本信息,就可以轻松地完成上述的操作。
from icecream import ic
def hello(user:bool):
if user:
ic()
else:
ic()
hello(user=True)
输出结果如下。
ic| ice_1.py:5 in hello() at 02:34:41.391
从输出结果看,函数hello中的第5行的代码已被执行,而第7行的代码未执行。
03. 自定义前缀
如果您想在打印语句中插入自定义前缀(例如代码执行时间),icecream也是能实现的。
from datetime import datetime
from icecream import ic
import time
from datetime import datetime
def time_format():
return f'{datetime.now()}|> '
ic.configureOutput(prefix=time_format)
for _ in range(3):
time.sleep(1)
ic('Hello')
输出结果如下。
2021-01-24 10:38:23.509304|> 'Hello'
2021-01-24 10:38:24.545628|> 'Hello'
2021-01-24 10:38:25.550777|> 'Hello'
可以看到代码的执行时间,就显示在输出的前面。
04. 获取更多的信息
除了知道和输出相关的代码之外,你可能还想知道代码执行的行和代码文件。
在ic.configureOutput()中,设置includeecontext的参数值为True即可。
from icecream import ic
def plus_five(num):
return num + 5
ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
输出结果如下。
ic| ice_test.py:7 in <module>- plus_five(4): 9
ic| ice_test.py:8 in <module>- plus_five(5): 10
这里我们就知道了,第一个输出是由函数plus_five在文件icecream_example.py的第7行执行的。
第二个输出则是由函数plus_five在代码文件的第8行执行的。
上述两个操作都用到了ic.configureOutput()函数。
通过查看源码,可知有四个可供设置的参数。
prefix,自定义输出前缀
outputFunction,更改输出函数
argToStringFunction,自定义参数序列化字符串
includeContext,显示文件名、代码行、函数信息
05. 删除Icecream代码
最后你可以将icecream仅用于调试,而将print用于其他目的(例如漂亮的打印)。
from icecream import ic
def plus_five(num):
return num + 5
ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
for i in range(10):
print(f'****** Training model {i} ******')
输出结果。
ic| ice_1.py:7 in <module>- plus_five(4): 9
ic| ice_1.py:8 in <module>- plus_five(5): 10
****** Training model 0 ******
****** Training model 1 ******
****** Training model 2 ******
****** Training model 3 ******
****** Training model 4 ******
****** Training model 5 ******
****** Training model 6 ******
****** Training model 7 ******
****** Training model 8 ******
****** Training model 9 ******
由于你可以区分调试打印和漂亮打印,因此搜索和删除所有ic调试语句非常容易。
删除所有调试代码后,你的Python代码就整洁了。
#### 近期文章
####
**[Python网络爬虫与文本数据分析** ](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650076662&idx=2&sn=95c13e697ecc4135a9cf4d494997717f&chksm=f1f744e9c680cdff1880531dffd424262cc01d738a38887d1a2d85158155b3bb4511bd237bd2&scene=21#wechat_redirect)**[bsite库 | 采集B站视频信息、评论数据](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074940&idx=1&sn=b920d974cb0ba4f6d577cbf30253fb46&chksm=f1f75e23c680d7355ac0ce7293cc35c75251be283cd5b47436b3f803e76f9f3b979a445d4534&scene=21#wechat_redirect)**
**[爬虫实战 | 采集&可视化知乎问题的回答](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650075913&idx=2&sn=688bd03aa845ac8fa29fb92abd6cc3d0&chksm=f1f74216c680cb00ebe3e47847b519dccf743b9c34461a5688ae4ecfa4f18ebbdaa3c12525de&scene=21#wechat_redirect)** **** [pdf2docx库 | 转文件格式,支持抽取文件中的表格数据](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650076027&idx=2&sn=417dcf26c9e50fdbcfaa7db3f2c8e6b6&chksm=f1f74264c680cb72a3cddb8c73cb2f7fba8c0ddc57168043472b6aee7b8f4fa13ea19e474907&scene=21#wechat_redirect)**** [rpy2库 | 在jupyter中调用R语言代码](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074407&idx=1&sn=11fe665fb26d6519a8738b3cca9af527&chksm=f1f75c38c680d52eb7e27110fdd39d81894f76502a81c1141e2de4f24f5f6894d4ccf94e9f84&scene=21#wechat_redirect)[tidytext | 耳目一新的R-style文本分析库](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074453&idx=1&sn=00e4e7851290de8eb0f70256fb64f023&chksm=f1f75c4ac680d55c2c8adcece93258be0953de8cb5338557275475c6baacdc83cc18dcb4d74a&scene=21#wechat_redirect)[reticulate包 | 在Rmarkdown中调用Python代码](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074512&idx=1&sn=b096ab63bc2f39df8f7d275906962004&chksm=f1f75c8fc680d599b329d6485dcb2579e837d87287d915101b189b4b1efb01304acd71e657f9&scene=21#wechat_redirect)[plydata库 | 数据操作管道操作符>>](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074438&idx=1&sn=3a16075de9eebed9c2c0010a95a1f177&chksm=f1f75c59c680d54f239c6d2a1824c1bec3c8d860e9972c0b0d2cd235e903e49f69cfddf6b510&scene=21#wechat_redirect)[plotnine: Python版的ggplot2作图库](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074005&idx=1&sn=7bca7b5bf7cc6cbf748f7f873dfb74f6&chksm=f1f75a8ac680d39c6d50ad3ec6f99ac4ab67df1f133aecfda92dc3ef06c1261e0b58c0cbd0ce&scene=21#wechat_redirect)
[七夕礼物 | 全网最火的钉子绕线图制作教程](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074387&idx=1&sn=76b0fdf05a2a72a960e5ef56234c5ea9&chksm=f1f75c0cc680d51a1490724910d9c58f3d61718020a95a3e0083a91399e51e3d014c75089dd0&scene=21#wechat_redirect)
[读完本文你就了解什么是文本分析](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650073067&idx=1&sn=ccffb97b7693eb128c0c4715fc5f1787&chksm=f1f756f4c680dfe26db49614fb5e4fe54500e95c103f844707776598a6d9362dff9141a6f18e&scene=21#wechat_redirect)
[文本分析在经管领域中的应用概述](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650073829&idx=1&sn=68650725b0b7c93ca46eb3d9c0a7f2f6&chksm=f1f75bfac680d2ecb5a8c3565241ce3edb0e8d06c04f0a5a5243ba887d2daf02651329f88e28&scene=21#wechat_redirect) [综述:文本分析在市场营销研究中的应用](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650073220&idx=1&sn=02dca6c2b0de0ca36b94e3410cbda097&chksm=f1f7599bc680d08d9203c8deb8fc223500d48c5bf940cae1e5b2098d36ad27ec3a62de9cc87a&scene=21#wechat_redirect)
[plotnine: Python版的ggplot2作图库](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650074005&idx=1&sn=7bca7b5bf7cc6cbf748f7f873dfb74f6&chksm=f1f75a8ac680d39c6d50ad3ec6f99ac4ab67df1f133aecfda92dc3ef06c1261e0b58c0cbd0ce&scene=21#wechat_redirect)[小案例: Pandas的apply方法](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650073684&idx=1&sn=5ade4b5d8952b476b19f6d329c036b16&chksm=f1f75b4bc680d25db06a16a3056d94f6fb24a0dd4db314b37e964d7d3c9c368ac61992818c32&scene=21#wechat_redirect) [stylecloud:简洁易用的词云库](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650073991&idx=1&sn=5e0d2af9bba4b795396425b5dc559e41&chksm=f1f75a98c680d38ef4191cdd198a6d68ec620f36a64f83aaf6ffe9d028ed6b6e568509be8726&scene=21#wechat_redirect) [用Python绘制近20年地方财政收入变迁史视频](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650073941&idx=1&sn=e8c874831a3bd57f0ff5ce3d5ffa2d1f&chksm=f1f75a4ac680d35cd1d02417796204e2edb1d88330c32074abc30c447dcc6aad252f7516fe79&scene=21#wechat_redirect) [Wow~70G上市公司定期报告数据集](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650072580&idx=1&sn=9d93854c5b9e03928c9c1036d05bc094&chksm=f1f7571bc680de0d1ce1677524cd77f5a5c5f2d80bacf807ff61850d99b66310680d62b3041b&scene=21#wechat_redirect)
[漂亮~pandas可以无缝衔接Bokeh](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650072170&idx=1&sn=6fe99c94dd3e2219ee31a4e57f9bce9c&chksm=f1f75575c680dc63fe0c04849d4dfce1cd19ebe2ae120b3b65541b603aa791999654df3a0125&scene=21#wechat_redirect) [YelpDaset: 酒店管理类数据集10+G](http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA==&mid=2650071871&idx=1&sn=1ac8c58b3f8ce9ef3bca31bed97700ed&chksm=f1f75220c680db36d6a4c12cdcef17dbb9cc640f832e9d0a7e53bc22aaf1f2f451c17f2b8002&scene=21#wechat_redirect)
“ 分享 ”和“ 在看 ”是更好的支持!
PS: 如本文对您有疑惑,可加QQ:1752338621 进行讨论。
0 条评论