1

Hey so right now I am trying to make a small program that can delete lines based off of number in front of the question. (Just so you don't have to retype the whole question again)

with open("DailyQuestions.txt", "r") as f:
        lines = f.readlines()
      with open("DailyQuestions.txt", "w") as w:
        for line in lines:
          Num, A = line.split(" - ")
          if not line.startswith(Num):
            w.write(line)

Textfile:

1 - Q1
2 - Q2
3 - Q3
4 - Q4
5 - Q5

The problem with this is that it either deletes the whole file or it it expects 2 values (Num, A = line.split(" - ")). I still can't figure out a way for it to just delete the whole line based on the number infront of it. Any tips or suggestions would help a lot!

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
SpaceTurtle0
  • 163
  • 3
  • 18

5 Answers5

0

Two things:

First, you can use one open by passing in r+, this can tidy up your code. Secondly, w overwrites the entire file, it doesn't append. If you would like to make it append, you can pass in a instead of w to open. I recommend, assuming you aren't dealing with a large amount of data, storing what you want to write in a string variable (with newlines), and then writing that to the file.

Here is a solution:

toWrite = "";
with open("DailyQuestions.txt", "r") as f:
    lines = f.readlines()
    for line in lines:
        Num, A = line.split(" - ")
        if not Num == "Number to Replace":
            toWrite += line
with open("DailyQuestions.txt", "w") as w:
    w.write(toWrite)
Saddy
  • 1,515
  • 1
  • 9
  • 20
0

Your primary problem lies in the construction of your Boolean Expression. Based on the current design of the program, the expression will always be False. Therefore nothing is outputted.

Before you search through all the lines of text, you need to create a variable to store the numbers that you want to exclude. Use a List or Set:

numbers_to_exclude = {'2', '4', '6', '5'}

Then utilize this variable in your Boolean Expression:

if first_character_of_line not in numbers_to_exclude: 

Your final code should look something like:

numbers_to_exclude = {'2', '4', '6', '5'}

with open("DailyQuestions.txt", "r") as f:
    
    lines = f.readlines()
      
    with open("output.txt", "w") as w:
        
        for line in lines:
            
            first_char_in_line = line[0]
            
            if first_char_in_line not in numbers_to_exclude: //The line begins with the number we want to output
                
                w.write(line)
Blake G
  • 581
  • 7
  • 24
0

As an option, read all lines from the file and the generator go through each, rewrite the file. If you need to delete all lines where there is no number

with open('test_file.txt', 'r') as f:
    #Delete line if not have -
    lines = [value[value.find('-')+1:].strip() for value in f.readlines() if value.find('-') != -1]

with open('test_file.txt', 'w') as f:
    if lines: f.write('\n'.join(lines))
-1

I dont really know how to code it but i can maybe to give you an idea. My idea is to create all again by saving the text or all the line on a variable like a list and next rewrite it ( with a “for i in range(len()) :” loop for example) but with a “if” that took the first number and check if it is the number you want to delete, so you just have to dont write it. The code will be not super optimize but i think that can work

I hope i can help you :)

SkiBoxing
  • 42
  • 3
-1

If you are just trying to remove the lines from the file that starts with a specific number you could use this code:

rewrite = ""
exluded_num = 4 #Place your excluded number here
with open("DailyQuestions.txt", "r") as f:
    for line in f:
        Num, A = line.split(" - ")
        if int(Num) != exluded_num:
            rewrite += line

open("DailyQuestions.txt", "w").write(rewrite)

The problem with the code you posted was that you are altering the text file as you are trying to read through it. That will throw an exception.

Nomissimon10
  • 102
  • 1
  • 11