0

I'm trying to remove a couple of lines from a text file that I imported from my Kindle. The text looks like:

Shall I come to you?
Nicholls David, One Day, loc. 876-876


Dexter looked up at the window of the flat where Emma used to live.
Nicholls David, One Day, loc. 883-884


I want to grab the bin bag and do a forensics
Sophie Kinsella, I've Got Your Number, loc. 64-64

The complete file is longer, this is just a piece of document. The aim with my code is to remove all lines where "loc. " is written so that just the extracts remain. My target can be also seen as removing the line which is just before the blank line.

My code so far look like this:

f = open('clippings_export.txt','r', encoding='utf-8')
message = f.read()
line=message[0:400]
f.close()

key=["l","o","c","."," "]


for i in range(0,len(line)-5):
    if line[i]==key[0]:
        if line[i+1]==key[1]:
            if line[i + 2]==key[2]:
                if line[i + 3]==key[3]:
                    if line[i + 4]==key[4]:

The last if finds exactly the position (indices) where each "loc. " is located in file. Nevertheless, after this stage I do not know how to go back in the line so that the code catches where the line starts, and it can be completely remove. What could I do next? Do you recommend me another way to remove this line?

Thanks in advance!

little_mice
  • 53
  • 1
  • 6

1 Answers1

3

I think that the question might be a bit misleading!

Anyway, if you simply want to remove those lines, you need to check whether they contain the "loc." substring. Probably the easiest way is to use the in operator.

Instead of getting whole file from read() function, read the file line by line (using the readlines() function for example). You can then check if it contains your key and omit it if it does.

Since the result is now list of strings, you might want to merge it: str.join().

Here I used another list to store desired lines, you can also use "more pythonic" filter() or list comprehension (example in similar question I mentioned below).

f = open('clippings_export.txt','r', encoding='utf-8')
lines = f.readlines()
f.close()

filtered_lines = []
for line in lines:
    if "loc." in line: 
        continue
    else:
        filtered_lines.append(line)

result = ""
result = result.join(filtered_lines)

By the way, I thought it might be a duplicate - Here's question about the opposite (that is wanting lines which contain the key).

Adrian
  • 105
  • 2
  • 6
  • Hi Adrian. Probably the question was a bit misleading but your answer perfectly worked! The thing is that I didn't know anything about the command deadline (I just knew about read() ). Thanks a lot! – little_mice Dec 03 '17 at 09:47
  • That's why nobody answered it before and I could get my first answer :) I'm glad I could help! – Adrian Dec 03 '17 at 11:48