python - 如何删除每行特殊字符串之后的字符串
问题描述
我尝试使用 python 将文本转换为 new_text。我无法在以下句子中的 'metamorphemes=' 之后匹配。我怎样才能做到这一点?
text = "I have no idea about metamorphemes=sentence. I have idea about metamorphemes=python."
输出
new_text = "I have no idea about. I have idea about."
解决方案
使用re.sub
,我们可以从每个句子中删除 key=value 对:
text = "I have no idea about metamorphemes=sentence. I have idea about metamorphemes=python."
output = re.sub(r'\s*\w+=\w+', '', text)
print(output)
这打印:
I have no idea about. I have idea about.
推荐阅读
- git - 如何将命令设置为另一个命令 powershell?
- javascript - 在 Meteor.js 中更新帖子
- python - 如何按熊猫数据框或python中的颜色排序
- math - 如何在 Coq 中证明逻辑等价?
- c# - Windows 7,在 Windows 启动时或程序关闭时启动程序
- git - Git 可以用于 SIMAN 等非标准编程吗?
- java - 如何在没有名称或ID的Java中单击selenium中的按钮
- fortran - 如何在 Fortran 中为复杂的 3d 数组分配指针
- java - 如何在android库中包含内部jar文件
- requirejs - formvalidation v1.3.0 不适用于 requirejs