python - Delete certain text pattern in python
问题描述
I'm trying to get rid of a certain pattern of text in my .txt file, which looks something like:
mystring = '''
example deletion words
in the first block
First sentence to keep.
example deletion words
in the second block
Second sentence to keep.
example deletion words
in the third block
Third sentence to keep.
example deletion words
in the fourth block'''
My desired output would look like:
"First sentence to keep.
Second sentence to keep.
Third sentence to keep."
So what I'm trying to do is get rid of all text between the strings "example" and "block", including the strings themselves. Any idea how I would go about that in either R or Python?
Sorry for forgetting to include my attempt with regex and just asking out of the blue and thanks to the people who took the effort to answer regardless. My working solution using regex and re package in python:
import re
cleanedtext = re.sub('\nexample.*?block','',mystring, flags=re.DOTALL)
print(cleanedtext)
解决方案
在 R 中,您可以使用str_remove_all
fromstringr
stringr::str_remove_all(string, "example.*block")
#[1] " First sentence to keep.\nSecond sentence to keep.\nThird sentence to keep.\n"
这是简写
stringr::str_replace_all(string, "example.*block", "")
数据
string <- "example deletion words in the first block First sentence to keep.
example deletion words in the second blockSecond sentence to keep.
example deletion words in the third blockThird sentence to keep.
example deletion words in the fourth block"
推荐阅读
- c# - 在 Puppeteersharp 中设置/更改/更改文本层
- javascript - 反应状态,Onclick第一次不会触发..只有第二次
- amazon-web-services - 如何在 docker 镜像中使用 AWS cli?
- json - 如何使用 Python 检查 JSON 字符串以获取列表?
- sql-server - 多个队列的 Service Broker 问题
- javascript - 数据表,使用下拉值导出到 excel
- react-dnd - react-dnd monitor.getItem() 总是等于悬停的项目
- visual-studio-code - 如何在 VS 代码中创建多个光标(通过使用键盘快捷键)?
- python - Python:以递归方式快速排序
- javascript - 如何将jquery代码行转换为vue