Reputation:
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
Upvotes: 1
Views: 144
Reputation: 669
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
Upvotes: 1
Reputation: 11
def f(text):
return '\n'.join([t for t in text.lower().splitlines() if not '-agent' in t])
Upvotes: 1
Reputation: 36
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
Upvotes: 1