我有一个文本文件,其中包含大量文本和块,如下所示:
text text text text text text text text text
TMP [%] [KT] [PR] [SF]
1 0.10020 -0.0000 -60.0 0.0000
2 14.12826 0.0000 0.0 0.0000
3 4.00802 -120.3636 -6.0 191.5646
4 4.80962 0.0000 0.0 0.0000
text text text text text text text text text
我想读取1-4中的行并将这些值转储到某个数组(列方式/行方式),以便我以后可以使用它们。
tr=4 #number of rows after row starting with "TMP"
f =open("test.txt", "r")
fr = f.readlines()
f.close()
for i in len(tr):
for line in fr:
if line.startswith(' TMP'):# neglect other text and start with line containing TMP
line=line+1
print line
不知何故,这似乎不起作用。我所做的是检测到一条具有“TMP”的行并向前移动以读取接下来的4行。如何阅读如上所述的4行?
答案 0 :(得分:2)
我怀疑你想要实现的是,如果行以'TMP'
或' TMP'
开头并且继续只读取tr
行数,则开始读取行,即:
interesting_text = false
for line in fr:
if line.startswith(' TMP'): # neglect other text and start with line containing TMP
interesting_text = true
elif interesting_text and tr > 0:
print line
tr -= 1
elif tr == 0:
break
答案 1 :(得分:0)
您不应该首先嵌套这些循环,并且您需要xrange()
而不是len()
。此外,您不需要手动增加行计数器(这是我怀疑您line = line + 1
的意图 - 这无论如何都无法工作,因为line
是一个字符串)。< / p>
此外,将整个文件读入数组是没有意义的。以下内容更为简单:
tr = 4
with open("test.txt", "r") as f:
for i in xrange(tr): next(f) # skip four lines
for line in f:
if line.startswith(' TMP'): # only print lines containing TMP
print line
答案 2 :(得分:0)
这是另一种解决方案:
n = 4 # number of rows
with open('test.txt', 'r') as f:
lines = f.xreadlines()
line = next(lines)
while 'TMP' not in line:
line = next(lines)
line = next(lines) # skip the TMP line
for _ in xrange(n):
print line
line = next(lines)
答案 3 :(得分:0)
它不漂亮,但它会起作用:
with open('test.txt') as f:
start = 0
useful = []
for ln in f:
if ln.strip().startswith('TMP'):
start = 1
elif start and start < 5:
useful.append(ln)
start += 1
elif start > 4:
break
答案 4 :(得分:0)
这个怎么样?
data=[]
for index,line in enumerate(fr):
if line.startswith('TMP'):# neglect other text and start with line containing TMP
for i in range(4):
data.append(fr[index+i+1])
现在,您可以使用数据执行任何操作。