im have an array of strings where every position has a full paragraph as string.
im trying to delete a line from every paragraph in that string if they pass the condition
for example:
arr[0]= "A -agent: this line will be deleted
client: this will remain the same
A -agent: this will be deleted "
im using this code to delete the whole line where the agent speaks but is not working
def findAgentName(text):
words= text.lower().split()
agentName=[item for item in words if item[-6:] == '-agent']
if agentName != None:
agentName=agentName[:len(agentName)-6]
return "\n".join([x.strip() for x in text.splitlines() if agentName not in x])
else:
return
u can have a try like this:
arr = """A -agent : this line will be deleted
client: this will remain the same
A -agent: this will be deleted """
print(arr)
# A -agent : this line will be deleted
# client: this will remain the same
# A -agent: this will be deleted
import re
agentName = "-agent"
regex = re.compile(rf".*{agentName}.*(\n)?")
new_arr = regex.sub('', arr)
print(new_arr)
# client: this will remain the same
Hi you can also try in this way:
string = """A -agent : this line will be deleted
client: this will remain the same
A -agent: this will be deleted"""
list_string = string.lower().split("\n")
string_to_return = "\n".join(filter(lambda x: not "-agent" in x,list_string))
print(string_to_return)
#client: this will remain the same
def f(text):
return '\n'.join([t for t in text.lower().splitlines() if not '-agent' in t])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.