Home > front end >  Removing a word from a list
Removing a word from a list

Time:11-18

So I got a list with names and I'm trying to delete the names out of the list. but somehow it doesn't work the way I want it to. Any help would be much appreciated. I think the problem is something with the replace function but I'm not sure.

Funtion:

@pyqtSlot(str, str)
def rapportmail (self, email, wachtwoord):

    credentials = Credentials(email, wachtwoord)
    acc = Account(email, credentials=credentials, autodiscover=True)

    cursor.execute("SELECT DISTINCT Naam FROM Klant")
    updatedklant = str(cursor.fetchall())
    
    test = updatedklant

    for item in acc.inbox.all().order_by('-datetime_received')[:500]:

        inboxmail = str(item.sender).split("'")
        currentinboxmail = inboxmail[3]

        cursor.execute("SELECT DISTINCT Klant FROM Mail WHERE Mail=?", currentinboxmail)
        currentklant = str(cursor.fetchall())

        remove_characters = ["(",")",",","]","["]
        for characters in remove_characters:
            currentklant = currentklant.replace(characters, "")

        if currentklant not in updatedklant:

            for idx, elem in enumerate(test):
                if elem[0] == currentklant:
                    test.pop(idx)

It is prints this now:

currentklant == 'alerttestting'

test == [('alerttestting', ), ('emretestingsystems', ), ('jarnodebaas', ),('yikes', )]

Result should be:

currentklant == 'alerttestting'

test ==[('emretestingsystems', ), ('jarnodebaas', ),('yikes', )]

CodePudding user response:

Never, ever convert the result of cursor.fetchall() to a str and then strip out the characters you don't like. This is going to be a huge source of trouble. What if the name itself contains one of those characters, like "Bond, James" or "O'Brien"?

Keep it as a Python object (probably a list or tuple) and process it in that form.

In this case you probably wanted something like this:

    ...
    updatedklant = cursor.fetchall()  # No more str()
    ...
            currentklant = cursor.fetchall())  # No more str()
            ...
            test = [klant for klant in updatedklant if klant != currentklant]

CodePudding user response:

should work perfectly

[('alerttestting',), ('emretestingsystems',), ('jarnodebaas',), ('yikes',)]

if __name__ == "__main__":
    currentklant = 'alerttestting'
    test = [('alerttestting',), ('emretestingsystems',), ('jarnodebaas',), ('yikes',)]
    for idx, elem in enumerate(test):
        if elem[0] == currentklant:
            test.pop(idx)

[('emretestingsystems',), ('jarnodebaas',), ('yikes',)]

  • Related