Python Forum
How to write the condition for deleting multiple lines?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write the condition for deleting multiple lines?
#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

I'm not sure how to write the condition for deleting the A/B/C/D option together with the question entered by user.

Here is my code:
import os
def main():
    
    import os

    answer = input("Enter question to delete:")
    with open("testing.txt", "r") as read:
        with open("temp.txt", "w") as output:
            # iterate all lines from file
            for line in read:
                # if text matches then don't write it
                if line.strip("\n") != answer:
                    output.write(line)
                        
    
    read.close()
    output.close()
    # replace file with original name
    os.replace('temp.txt', 'testing.txt')

main()
Reply
#2
(Jul-10-2022, 05:36 AM)Lky Wrote: 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

I'm not sure how to write the condition for deleting the A/B/C/D option together with the question entered by user.

Here is my code:
import os
def main():
    
    import os

    answer = input("Enter question to delete:")
    with open("testing.txt", "r") as read:
        with open("temp.txt", "w") as output:
            # iterate all lines from file
            for line in read:
                # if text matches then don't write it
                if line.strip("\n") != answer:
                    output.write(line)
                        
    
    read.close()
    output.close()
    # replace file with original name
    os.replace('temp.txt', 'testing.txt')

main()

What are you expecting the user to input ? the number of the question ? or the matching text of the full question they want to remove ?
Reply
#3
(Jul-10-2022, 02:25 PM)jcrubaugh45 Wrote:
(Jul-10-2022, 05:36 AM)Lky Wrote: 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

I'm not sure how to write the condition for deleting the A/B/C/D option together with the question entered by user.

Here is my code:
import os
def main():
    
    import os

    answer = input("Enter question to delete:")
    with open("testing.txt", "r") as read:
        with open("temp.txt", "w") as output:
            # iterate all lines from file
            for line in read:
                # if text matches then don't write it
                if line.strip("\n") != answer:
                    output.write(line)
                        
    
    read.close()
    output.close()
    # replace file with original name
    os.replace('temp.txt', 'testing.txt')

main()

What are you expecting the user to input ? the number of the question ? or the matching text of the full question they want to remove ?
User have to enter full questions
Reply
#4
(Jul-10-2022, 02:27 PM)Lky Wrote:
(Jul-10-2022, 02:25 PM)jcrubaugh45 Wrote: What are you expecting the user to input ? the number of the question ? or the matching text of the full question they want to remove ?
User have to enter full questions
import os
def main():
    
    list3=[]
    ans = input("Delete:")
    with open("testing.txt", "r") as read:
        #file_content = read.readlines()
    
        with open("temp.txt", "w") as output:
        # iterate all lines from file
            
            for line in read:
                if line.strip() == ans:  # Or whatever test is needed
                    break
                    # Reads text until the end of the block:
            for line in read:  # This keeps reading the file
                if line[0].isupper():
                    break
                #print(line)
                strip_lines=line.strip()
                list1=strip_lines.split("\n")
                
            
                
                list3.append(list1)
            
            
                
            for line in read:
                currentline = line.strip('\n')
                if line not in list3:
                    output.write(line)
                    
            
    read.close()
    output.close()
    
    os.replace('temp.txt', 'testing.txt')
main()
Here is my latest code. (Assuming the option is in small letter (a./b./c./d.)
BashBedlam likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete multiple lines from txt file Lky 6 2,283 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  How to map two data frames based on multiple condition SriRajesh 0 1,474 Oct-27-2021, 02:43 PM
Last Post: SriRajesh
  Display table field on multiple lines, 'wordwrap' 3python 0 1,768 Aug-06-2021, 08:17 PM
Last Post: 3python
  pulling multiple lines from a txt IceJJFish69 3 2,571 Apr-26-2021, 05:56 PM
Last Post: snippsat
  multiple condition if statement problem FelixReiter 3 2,595 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Deleting multiple variables/arrays Robotguy 0 1,495 Aug-18-2020, 09:56 PM
Last Post: Robotguy
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,818 Aug-10-2020, 11:01 PM
Last Post: medatib531
  else condition not called when if condition is false Sandz1286 10 5,850 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,911 Jun-01-2020, 06:00 PM
Last Post: penahuse
  print python json dump onto multiple lines lhailey 2 19,823 Mar-02-2020, 12:47 PM
Last Post: vishalhule

Forum Jump:

User Panel Messages

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