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
#2
If I look how file is structured - isin't it simpler to drop all rows until first empty row is encountered and do something with remaining rows? Something like:

with open('books_test.txt', 'r') as f:
    for row in f:
        if row in ['\n', '\r\n']:
            break
    for row in f:
        # do something
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,157 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Delete multiple lines from txt file Lky 6 2,348 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 1,614 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,734 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Editing text between two string from different lines Paqqno 1 1,333 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  failing to print not matched lines from second file tester_V 14 6,194 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,105 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Find and delete above a certain line in text file cubangt 12 3,559 Mar-18-2022, 07:49 PM
Last Post: snippsat
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,068 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  How to delete portion of file already processed? Mark17 13 2,805 Jan-22-2022, 09:24 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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