0

I have text file where is probably sometimes one line too much and i have to delete it. Not always but still need to check it everytime.

The phrase always includes the same words at the beginning, but end of the line are maybe different, howefever full line need to delete.

Example:

This is original lines in middle of txt file:

.........
<br>rrrrr TTTTTT ffgggggggg
<br>ja UOOOOOOOO on >= 16 täysin.
<br>ja numeroyhdistelmä on 9- 39- 9
<br>ja href="./reeeee.html">wwwwjjhjhkkghjky. </a> </td>
</tr></TABLE>
<table border=0 cellpadding= 25 width= 560><TR><TD width=80></TD><TD 
width=240><PRE>
.........   

after python code lines would be:

.........
<br>rrrrr TTTTTT ffgggggggg
<br>ja UOOOOOOOO on >= 16 täysin.
<br>ja href="./reeeee.html">wwwwjjhjhkkghjky. </a> </td>
</tr></TABLE>
<table border=0 cellpadding= 25 width= 560><TR><TD width=80></TD><TD 
width=240><PRE> 
.........

So line what need delete is:

    <br>ja numeroyhdistelmä on 9- 39- 9

If i use letter "ä" to the code it gives some "unicode" errors but i havent choice try something else word to search because beginning the line are somewhere else too and values "9- 39- 9" probably change.

This what i was try:

f = open("text2.txt","r+")
d = f.readlines()
f.seek(0)
for line in d:
    if "numeroyhdistelmä" in line:
        f.write(line)
f.truncate()
f.close()

I think letter "ä" is not only problem because i was testing this code some other search word and it delete all lines in a text file.

Thanks!

Thomas
  • 11
  • 3
  • What is an unicode error you are getting? – Talita Jul 12 '17 at 11:35
  • SyntaxError: Non-UTF-8 code starting with '\xe4' in file test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details – Thomas Jul 12 '17 at 20:19

3 Answers3

0

I would readlines check line, if "word to delete exist" remove line else write to file.

with open("file") as data:
    lines = data.readlines()

with open("file","w") as f:
    for line in lines:
        if "word to remove" in line:
            continue
        f.write(line,"\n")
Harwee
  • 1,601
  • 2
  • 21
  • 35
0

Here is how I might solve this problem - also here a question about using the with syntax, which is preferred to use when opening and closing a file: Why is with open() better for opening files in Python?

filename = 'text2.txt'
with open(filename, 'r+') as txt_file:
    temp = txt_file.readlines()
    txt_file.seek(0)

    for line in temp:
        if not 'numeroyhdistelm' in line:
            txt_file.write(line)

    txt_file.truncate()
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
flevinkelming
  • 690
  • 7
  • 8
0

You are now saving only lines with 'numeroyhdistelmä', you should add 'not' to the loop. It is also a better practice to use with open() then open() and close().

wordFlag = 'numeroyhdistelmä'
with open("text2.txt","r+") as f:
    lines = f.readlines()

with open("text2.txt","w") as f:
    for line in f:
        if not wordFlag in line:
            f.write('line')
    f.truncate()

You are getting encoding error because test2.txt file is not utf-8 encoded. If you care about special characters you should decode your file while opening it. There are encode() and decode() functions avaliable for strings but I prefer to use codecs module. I am guessing encoding of your file is Latin, but you shoud check it and change the variable if needed. So then your code would look like:

import codecs

encoding = 'Latin'
wordFlag = 'numeroyhdistelmä'
with codecs.open('text2.txt', 'r', encoding) as f:
    lines = f.readlines()

with open('text2.txt','w') as f:
    for line in lines:
        if not wordFlag in line:
            f.write(line)
    f.truncate()
Talita
  • 805
  • 3
  • 11
  • 31
  • SyntaxError: Non-UTF-8 code starting with '\xe4' in file test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details <---This because there are "ä" letter – Thomas Jul 12 '17 at 20:20