1

I have a file F, content huge numbers e.g F = [1,2,3,4,5,6,7,8,9,...]. So i want to loop over the file F and delete all lines contain any numbers in file say f = [1,2,4,7,...].

F = open(file)
f = [1,2,4,7,...]
for line in F:
    if line.contains(any number in f):
        delete the line in F
4
  • A good answer that's not specific to python is in stackoverflow.com/questions/7152250/…. The short version is "you can't 'just delete' part of a file" Commented Sep 10, 2011 at 13:53
  • Does the file contain comma-delimited numbers on different lines or what? Commented Sep 10, 2011 at 13:59
  • 1, 2, 3, 4, 5, 6, 7, 8, 9 are not huge numbers Commented Sep 10, 2011 at 15:06
  • What do you mean by delete in this case? Is the file indexed or just a text file? Commented Sep 10, 2011 at 21:14

4 Answers 4

3

You can not immediately delete lines in a file, so have to create a new file where you write the remaining lines to. That is what chonws example does.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not clear to me what the form of the file you are trying to modify is. I'm going to assume it looks like this:

1,2,3
4,5,7,19
6,2,57
7,8,9
128

Something like this might work for you:

filter = set([2, 9])
lines = open("data.txt").readlines()
outlines = []
for line in lines:
    line_numbers = set(int(num) for num in line.split(","))
    if not filter & line_numbers:
        outlines.append(line)
if len(outlines) < len(lines):
    open("data.txt", "w").writelines(outlines)

I've never been clear on what the implications of doing an open() as a one-off are, but I use it pretty regularly, and it doesn't seem to cause any problems.

2 Comments

filter is just a set, how do you find the numbers in it in every line?
@F.C. he takes the intersection of that and the numbers in the line.
1
exclude = set((2, 4, 8))           # is faster to find items in a set
out = open('filtered.txt', 'w')
with open('numbers.txt') as i:     # iterates over the lines of a file
    for l in i:
        if not any((int(x) in exclude for x in l.split(','))):
            out.write(l)
out.close()

I'm assuming the file contains only integer numbers separated by ,

Comments

0

Something like this?:

nums = [1, 2]
f = open("file", "r")
source = f.read()
f.close()
out = open("file", "w")
for line in source.splitlines():
    found = False
    for n in nums:
        if line.find(str(n)) > -1:
            found = True
            break
    if found:
        continue
    out.write(line+"\n")
out.close()

1 Comment

you could just make for line in f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.