Python Forum
Compare element of list with line of file : if match, delete line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare element of list with line of file : if match, delete line
#1
Hi Smile

I have a list:
['123;G-12;http//www.g.ch/bidule_12', '456;G-422;http//www.g.ch/bidule_422', '789;G-562;http//www.g.ch/bidule_562']
I have a csv.file with following lines:
123;G-12;http//www.g.ch/bidule_12
456;G-422;http//www.g.ch/bidule_422
789;G-562;http//www.g.ch/bidule_562
breu
5555;G-0;http//www.g.ch/bidule_0

As a result, I want only these lines remaining in my csv.file:
breu
5555;G-0;http//www.g.ch/bidule_0

So, my python script is supposed to compare each element of my list with each line ; if they match, the line is deleted in the csv.file.

I tried (for a long time) loop + "replace" or "re.sub" with no success.

Any idea?

Thanks Smile
Reply
#2
What you have done so far?
Reply
#3
g_complet_resultat = open("g_complet_resultat.csv", "r") # file including the mentioned lines
NumeroLigne = 0
for line in g_complet_resultat: # mentioned list
    NumeroLigne_supprimees = 0
    for ligne_supprimee in g_lignes_supprimees_en_liste_result:
        g_cherche = re.search(str(ligne_supprimee), line)
        if g_cherche:
            g_replace = re.sub(line, '', line)

    NumeroLigne_supprimees = +1
NumeroLigne = +1
g_resultat.close()
Problem : content of g_complet_resultat.csv does not change (no lines deleted).
Reply
#4
Quote:Problem : content of g_complet_resultat.csv does not change (no lines deleted).

This is because you open it for read and not for write.
I suggest you to create a second file for the output to be more robust and not lose data while you're testing the script.

g_lignes_supprimees_en_liste_result = ['123;G-12;http//www.g.ch/bidule_12', '456;G-422;http//www.g.ch/bidule_422', '789;G-562;http//www.g.ch/bidule_562']
with open("g_complet_resultat.csv", "r") as f_in:
    with open("out.csv", "w") as f_out:
        for line in f_in:
            # Removes the '\n' to compare with elements in the list
            line = line[:-1]
            if line not in g_lignes_supprimees_en_liste_result:
                f_out.write(line + '\n')
Is it a requisite to modify the same file? If so, let me know.
Reply
#5
SO easy for you, gontajones.
I am grateful.
Clap
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No new line from print in a browser Lou 13 1,366 Dec-01-2025, 06:39 PM
Last Post: noisefloor
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 931 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,937 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
  I am a newbie I like to use the command line Mikel2025 1 1,063 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,237 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 967 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 1,940 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  removing one list element without using its index paul18fr 7 3,171 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 3,127 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020