Python Forum
Delete multiple lines from txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete multiple lines from txt file
#1
Does anyone know how to delete lines in txt file according to user input without using any list or dictionary?

For example content inside txt file:
Question 1 Which is correct?
A. X
B. XX
C. XXX
D. XXXX
Question 2 Which is wrong?
A. X
B. XX
C. XXX
D. XXXX
Question 3 Which is True?
A. X
B. XX
C. XXX
D. XXXX

When user choose to delete question 2, the file will updated and the output will become like this:
Question 1 Which is correct?
A. X
B. XX
C. XXX
D. XXXX
Question 3 Which is True?
A. X
B. XX
C. XXX
D. XXXX
Reply
#2
The best way to do this is:
  1. Copy the lines of the original file to a temporary file, but don't copy the lines you do not want.
  2. If no errors occurred: remove the original file.
  3. Rename the temporary file to the name of the original file.
Reply
#3
def remove_lines_list (file):
    ip_list = open(file).readlines()
    return [address.rstrip('\n') for address in ip_list]
I use this function to take a list of IP addresses and remove all the trailing \n so i get a clean list with no extra spaces or lines.. i dont know if this is helpful but i use this when getting a txt file in put of IP addresses i need to loop through.
Reply
#4
(Jul-09-2022, 06:00 PM)ibreeden Wrote: The best way to do this is:
  1. Copy the lines of the original file to a temporary file, but don't copy the lines you do not want.
  2. If no errors occurred: remove the original file.
  3. Rename the temporary file to the name of the original file.
What will be the condition for A. B. C. D.?
Reply
#5
(Jul-10-2022, 12:09 AM)jcrubaugh45 Wrote:
def remove_lines_list (file):
    ip_list = open(file).readlines()
    return [address.rstrip('\n') for address in ip_list]
I use this function to take a list of IP addresses and remove all the trailing \n so i get a clean list with no extra spaces or lines.. i dont know if this is helpful but i use this when getting a txt file in put of IP addresses i need to loop through.
It can't work as I need to delete the whole question not only one line.
Reply
#6
(Jul-10-2022, 05:32 AM)Lky Wrote: What will be the condition for A. B. C. D.?
Introduce a boolean variable e.g. "do_copy". If this variabel is true, copy the line. Else not.
When a line contains: "Question 2 " then toggle the variable to False.
When a line contains: "Question" then toggle the variable to True.
Reply
#7
That should work, as long as Question does not appear in any answers. Basically using a flag that is unique to the category of line Question.
Suggest also - do this in memory, with option of displaying output, then do the write after approval. Skips the swap part, or you can allow user to Save As with a different filename. Adds a little versatility
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write the condition for deleting multiple lines? Lky 3 1,100 Jul-10-2022, 02:28 PM
Last Post: Lky
  failing to print not matched lines from second file tester_V 14 5,946 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,857 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Find and delete above a certain line in text file cubangt 12 3,353 Mar-18-2022, 07:49 PM
Last Post: snippsat
  How to delete portion of file already processed? Mark17 13 2,635 Jan-22-2022, 09:24 AM
Last Post: Pedroski55
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,268 Dec-04-2021, 08:33 PM
Last Post: Ascalon
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,143 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Display table field on multiple lines, 'wordwrap' 3python 0 1,747 Aug-06-2021, 08:17 PM
Last Post: 3python
  Importing a function from another file runs the old lines also dedesssse 6 2,479 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [Solved] Trying to read specific lines from a file Laplace12 7 3,473 Jun-21-2021, 11:15 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

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