Python Forum
How to write the condition for deleting multiple lines? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to write the condition for deleting multiple lines? (/thread-37693.html)



How to write the condition for deleting multiple lines? - Lky - Jul-10-2022

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()



RE: How to write the condition for deleting multiple lines? - jcrubaugh45 - Jul-10-2022

(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 ?


RE: How to write the condition for deleting multiple lines? - Lky - Jul-10-2022

(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


RE: How to write the condition for deleting multiple lines? - Lky - Jul-10-2022

(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.)