1

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 

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

i have the agent name in a variable called agentName how would that look in re.compile(r".*-agent.*(\n)?") is it going to be e.compile(r".*+agentName+*(\n)?")
1

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

2 Comments

im getting this error 'in <string>' requires string as left operand, not list
Have you changed "-agent" with something else inside lambda function?
1
def f(text):
    return '\n'.join([t for t in text.lower().splitlines() if not '-agent' in t])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.