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
  element in list detection problem jacksfrustration 5 313 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Line graph with two superimposed lines sawtooth500 4 299 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  Compare current date on calendar with date format file name Fioravanti 1 209 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  How to add multi-line comment section? Winfried 1 192 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  break print_format lengthy line akbarza 4 355 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,220 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 480 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 374 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 299 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 702 Jan-16-2024, 03:45 AM
Last Post: johnywhy

Forum Jump:

User Panel Messages

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