简体   繁体   English

如何从文本文件中读取特定的行和列,以及如何使用python决定数值?

[英]How to read a specific row and column from a text file and base decision on numeric value with python?

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:"{}"'

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: 编辑这是您的与建议的mod的循环:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM