0

I have a number of text files in the form:

alpha 89687
beta  9564
delta 10000

I only want to look at each line individually and evaluate each value in the second column. If the value is not within a certain range I would like to delete the entire file. I have a text file with the titles of all the files which I want to go through. Here is my code:

with open('filetitles.dat', 'r') as f:
for line in f:
    with open(line, 'r+') as t:
        for i, enumerate(t):
            v = i == 2 and #how to specify column
                if v<1 or v>100:
                    delete(t)
            z = i == 3 and #how to specify column
                if v<100 or v>120000:
                    delete(t)

Any help would be greatly appreciated. Thanks Below is my modified code. I am having trouble getting it to go to the next line is the conditions aren't met.

import os
with open('test.txt', 'r') as f:  #file with titles of files
    files=[l.strip() for l in f]

toDel=[]
for file in set(files): 
    with open(file, 'r+') as t: #open specific file
        for line in t:
            v,w = line.split()[0:2]     #try to specify lines and columns   
            if type(v) == int and type(w) == float: #check only lines in specific format
                if int(v)==1000021  and float(w)<2.5 or float(w)>3: #arbitrary values which will ensure deletion of test file                   
                    toDel.append(file)
                else:
                    some command to go to next line
            else:
                some command to go to next line
for file in set(toDel):
    os.remove(file)     #delete files
    print 'Delete:"{}"'
user1431534
  • 13
  • 1
  • 1
  • 5
  • `open(line, 'r+')` - No, don't do this. You `open` files, not lines of text. `v = i == 2`. v becomes 1 if i is 2, otherwise v becomes 0. That doesn't seem to be what you intended. – Junuxx Jun 07 '12 at 20:06
  • @Junuxx It's `True` and `False`, not `1` and `0`, but good point anyway. – Lev Levitsky Jun 07 '12 at 20:10
  • @Lev: That's equivalent, see [here](http://stackoverflow.com/a/2764099/1404505). – Junuxx Jun 07 '12 at 20:11
  • You don't need anything else to go to the next line, that's what the `for` loop is used for. – Amr Jun 09 '12 at 21:36

1 Answers1

0

Something like this would work as a skeleton:

with open('filetitles.dat', 'r') as f:
    files=[l.strip() for l in f]

toDel=[]
for file in set(files):
    with open(file, 'r') as f:
        for line in f:                  # each line of the file 
            w,v = line.split()[0:2]     # w==col 1, v==col 2
            if w=='beta' and int(v)<100000:  # example test
                  toDel.append(file)

for file in set(toDel):                 # now delete all the files...
    # os.remove(file)                   # 'set()' eliminates dups
    print 'Delete: "{}"'.format(file)     

EDIT Here is your loop with suggested mods:

for file in set(files): 
    with open(file, 'r+') as t: #open specific file
        for line in t:
            # use the appropriate regex to validate the line you seek 
            match=re.search(r'^(\d+)\s+([-+]?[0-9]*\.?[0-9]+)\s*$',line)
            if match:
                v=int(match.group(1))
                w=float(match.group(2))
                if v==1000021  and (w<2.5 or w>3.0):                     
                    toDel.append(file)
the wolf
  • 34,510
  • 13
  • 53
  • 71
  • There are a couple of lines in my file which are not in the same format as the rest. I tried to get around that by adding an if statement to check if the v and w are correct variable types. However I can't figure out how to get the code to go to the next line in the file if the variable types are wrong. I edit my code in my original question to show the problem I am having. Thanks – user1431534 Jun 09 '12 at 20:54
  • @user1431534: `continue` is the statement that you seek, but it is better to construct the logic to fall through if there is not match to the line in the file. I have re-written (BUT NOT TESTED!!!) the loop in your edit. – the wolf Jun 10 '12 at 20:02