python 字符串替换功能 string.replace()可以用正则表达式,更优雅

说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的。

之前,我的代码写法如下,粗笨:

image

自从发现了正则表达式也生效后,代码变得优雅简洁:

image

备注:上图中的base_infopandas 里的 dataframe 数据结构,可以用上述方法使用 stringreplace 方法。

原文地址:https://www.cnblogs.com/jjliu/p/11514226.html