Input file:
$ cat data.txt
k:begin:0
i:0:66
i:1:76
k:end:0
k:begin:7
i:0:55
i:1:65
i:2:57
k:end:7
k:begin:2
i:0:10
i:1:0
k:end:7
k:begin:2
i:0:46
k:end:7
k:begin:9
i:0:66
i:1:56
i:2:46
i:3:26
k:end:7
Required: Print only first 3 instances of the above file. One instance being from "k:begin" to "k:end"
The python script:
import time,sys
if len(sys.argv) == 1:
sys.exit(0)
file=sys.argv[1]
fp = open(file, "rU")
lines = fp.readlines()
fp.close()
count=0
for line in lines:
f=line.split(":")
print line.rstrip()
if f[0]=="k" and f[1]=="end":
count=count+1
if count > 2:
break
Executing:
$ python printfirst3.py data.txt
k:begin:0
i:0:66
i:1:76
k:end:0
k:begin:7
i:0:55
i:1:65
i:2:57
k:end:7
k:begin:2
i:0:10
i:1:0
k:end:7
Related functions or concepts:
- Python readlines
0 Comments:
Post a Comment