次の形式のテキスト ファイルが多数あります。
alpha 89687
beta 9564
delta 10000
各行を個別に見て、2 番目の列の各値を評価したいだけです。値が特定の範囲内にない場合は、ファイル全体を削除したいと思います。調べたいすべてのファイルのタイトルを含むテキスト ファイルがあります。これが私のコードです:
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)
どんな助けでも大歓迎です。以下は私の変更されたコードです。条件が満たされていないため、次の行に進むことができません。
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:"{}"'