简体   繁体   English

python 从包含特定表达式的字符串中删除特定行

[英]python deleting specific lines from a string that contain specific expressions

i have the following string/text我有以下字符串/文本

streets_and_names = '''
#
street 49
 description someone lives there
 name Paul
#
street 51
 description no one lives there
 name no name
#
street 43
 description someone lives there
 name Mark
#
street 42
 description no one lives there
 name no name
#
'''

and i would like to get an output by removing the following lines from it我想通过从中删除以下行来获得 output

 description no one lives there
 name no name

and keeping the rest as it is.并保持 rest 不变。

does anyone has any idea?有人知道吗?

thank you.谢谢你。

If your example was literal and just did res = streets_and_names.replace(' description no one lives there\n name no name', '') , you would get the result you want.如果您的示例是字面意思并且只是做了res = streets_and_names.replace(' description no one lives there\n name no name', '') ,您会得到想要的结果。

result:结果:

'''
#
street 49
 description someone lives there
 name Paul
#
street 51
#
street 43
 description someone lives there
 name Mark
#
street 42
#
'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM