0

I'm trying to delete each line which contains "annote = {" but my code is not working.

I have a file stored in a variable myFile and I want to go through every line of this file and delete every line which contains the string annote.

this is basically my code:

    print(myFile.read())      //prints myFile
    myFile.seek(0)

    for lines in myFile:
        if b"annote = {" in lines:
            lines = lines.replace(b'.', b'')

    myFile.seek(0)
    print(myFile.read())    //this prints the exact same as the print method above so annote lines 
                            //haven't been removed from this file

I have no idea why annote lines doesn't get removed. There is probably anything wrong with the replace method because it always is inside the if request but nothing happens with annote lines. I've also tried lines.replace(b'.', b'') instead of lines = lines.replace(b'.', b'') but nothing happened.

Hope anyone can help me with this problem

Lennart
  • 67
  • 1
  • 10
  • 1
    `lines = lines.replace(b'.', b'')` only changes the in-memory content of `lines`, it does not effect the actual content of the file. To do that you need to write that back to the file – rdas Apr 28 '20 at 10:02
  • Do you know how to write that specific line back to the file? – Lennart Apr 28 '20 at 10:10
  • Does this answer your question? [Search and replace a line in a file in Python](https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python) – rdas Apr 28 '20 at 10:11
  • no. I tried almost every answer from this link. It delted "annote = {" but not the entire line. So if the line looke like this: "annote = {abc}" the terminal looks like this after running and printing those methods: "abc}". So only annote got deleted and not the entire line – Lennart Apr 28 '20 at 11:29

2 Answers2

0

This will do it for you.

f.readlines() returns a list of text lines

Then you check for the lines that do not contain the things you do not want

Then you write them to a separate new file.


    f2 = open('newtextfile.txt', 'w')

    with open('text_remove_line.txt', 'r') as f:
       for line in f.readlines():
           if 'annote = {' not in line:
               f2.write(line)
    f2.close()
Mo Huss
  • 434
  • 2
  • 11
  • Is not working. This only deletes the exact string "oldstring" and not the entire line which contains "oldstring" – Lennart Apr 28 '20 at 11:31
  • 1
    Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – today Apr 28 '20 at 23:13
0

This should work:

with open('input.txt') as fin :
    lines = fin.read().split('\n')  # read the text, split into the lines

with open('output.txt', 'w') as fout :
    # write out only the lines that does not contain the text 'annote = {'
    fout.write( '\n'.join( [i for i in lines if i.find('annote = {') == -1] ))
lenik
  • 23,228
  • 4
  • 34
  • 43