Python Forum
Can't seem to figure out how to delete several lines from a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't seem to figure out how to delete several lines from a text file
#1
I want to delete a block of text from a text file that looks like this:

Quote:1
Date stored: 2019-02-01;
Author name: l;
Book title: lll;
Quantity: 1;
Price: 2.

2
Date stored: 2019-01-02;
Author name: ghjk;
Book title: okj;
Quantity: 5;
Price: 4.

4
Date stored: 2019-01-02;
Author name: hello;
Book title: hekie;
Quantity: 1;
Price: 2.

1
Date stored: 2019-02-01;
Author name: k;
Book title: k;
Quantity: 4;
Price: 1.

1
Date stored: 2019-01-01;
Author name: o;
Book title: b;
Quantity: 4;
Price: 8.

5
Date stored: 2019-02-01;
Author name: dfgh;
Book title: iuhg;
Quantity: 8;
Price: 4.

But I only want to delete 1 block of text that starts with 1

So I have this code:

from itertools import groupby
    
    mainFileDelete = open("BooksTest.txt","r+")
    
    lst = []
    new = []
    
    for line in mainFileDelete:
        stripped = line.strip("\n")
        lst.append(stripped)
    
    #Makes a list into a list of lists
    def splitList(sequence, delimiter):
        return [list(g) for k, g in groupby(sequence, key = lambda x: x == delimiter) if not k]
    p = splitList(sequence = lst, delimiter = '')
    print(p)
    
    #Gets the sublists that starts with a number '1'
    for k in p:
        this=k
        if this[0] == '1':
            new.append(this)
    print(new)
    
    #Trys to make all of the list of lists into a string an then delete it from 
    #the text file
    for i in new:
        strinng = ''.join(i)
        print(strinng)
        for a in strinng:
            if a != strinng[1]:
                f.write(a)
        f.truncate()
    
    mainFileDelete.close()
It gives me all of the the blocks of text that start with 1 in a list of lists, like so:

[['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: k; ', 'Book title: k; ', 'Quantity: 4; ', 'Price: 1. '], ['1', 'Date stored: 2019-01-01; ', 'Author name: o; ', 'Book title: b; ', 'Quantity: 4; ', 'Price: 8.']]
And I want to delete just the second list from the list of lists. I know how to do it from the actual list. But I don't know how could I also delete it from the file.
I want this one gone from the text file:

Quote:1
Date stored: 2019-02-01;
Author name: k;
Book title: k;
Quantity: 4;
Price: 1.

This is the part from the whole code that should do it:

  #Trys to make all of the list of lists into a string an then delete it from 
        #the text file
        for i in new:
            strinng = ''.join(i)
            print(strinng)
            for a in strinng:
                if a != strinng[1]:
                    f.write(a)
            f.truncate()
But it doesn't work at all. Please help. It's the only thing I can't seem to figure out.
Thank you.
Reply


Messages In This Thread
Can't seem to figure out how to delete several lines from a text file - by Cosmosso - Dec-10-2019, 02:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,777 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Delete file with read-only permission, but write permission to parent folder cubei 6 25,399 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,000 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Delete multiple lines from txt file Lky 6 3,919 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 3,135 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,136 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Editing text between two string from different lines Paqqno 1 1,815 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  failing to print not matched lines from second file tester_V 14 8,955 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 5,489 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Find and delete above a certain line in text file cubangt 12 6,763 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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