本文实例讲述了Python实现简单过滤文本段的方法。分享给大家供大家参考,具体如下:
一、问题:
如下文本:
## Alignment 0: score=397.0 e_value=8.2e-18 N=9 scaffold1&scaffold106 minus 0- 0: 10026549 10007782 2e-75 0- 1: 10026550 10007781 8e-150 0- 2: 10026552 10007780 1e-116 0- 3: 10026555 100077780 0- 4: 10026570 100077680 0- 5: 10026579 10007758 4e-15 0- 6: 10026581 10007738 2e-44 0- 7: 10026587 10007734 9e-145 0- 8: 10026591 10007732 2e-147 ## Alignment 1: score=2304.0 e_value=1e-164 N=47 scaffold1&scaffold107 minus 1- 0: 10026836 10007942 2e-84 1- 1: 10026839 100079400 1- 2: 10026840 100079380 1- 3: 10026842 10007937 9e-82 1- 4: 10026843 10007935 7e-79 1- 5: 10026847 10007933 3e-119 1- 6: 10026850 10007932 2e-87 1- 7: 10026854 10007928 5e-22 1- 8: 10026855 10007927 3e-101 1- 9: 10026856 10007925 1e-106 1- 10: 10026857 100079240 1- 11: 10026858 10007922 9e-123 1- 12: 10026859 10007921 1e-80 1- 13: 10026860 10007920 8e-104 1- 14: 10026862 10007918 4e-25 1- 15: 10026863 100079170 1- 16: 10026864 10007912 4e-40 1- 17: 10026865 100079110 1- 18: 10026866 10007910 7e-122 1- 19: 10026867 10007908 2e-25 1- 20: 10026868 100079070 1- 21: 10026869 100079050 1- 22: 10026870 10007904 3e-150 1- 23: 10026871 10007903 5e-77 1- 24: 10026874 100079010 1- 25: 10026875 100078970 1- 26: 10026876 100078960 1- 27: 10026877 100078940 1- 28: 10026880 10007893 3e-52 1- 29: 10026881 100078920 1- 30: 10026882 100078910 1- 31: 10026883 100078900 1- 32: 10026886 10007889 1e-50 1- 33: 10026887 10007888 6e-157 1- 34: 10026888 100078870 1- 35: 10026889 100078840 1- 36: 10026890 10007883 2e-18 1- 37: 10026891 10007882 9e-64 1- 38: 10026892 100078810 1- 39: 10026895 100078800 1- 40: 10026898 100078750 1- 41: 10026900 100078740 1- 42: 10026901 100078730 1- 43: 10026902 10007871 2e-123 1- 44: 10026903 100078700 1- 45: 10026905 100078690 1- 46: 10026909 10007868 1e-81 ## Alignment 2: score=811.0 e_value=3.3e-43 N=17 scaffold1&scaffold111 minus 2- 0: 10026595 10007449 6e-40 2- 1: 10026599 10007448 4e-90 2- 2: 10026600 100074470 2- 3: 10026601 10007444 9e-55 2- 4: 10026603 10007438 4e-78 2- 5: 10026604 10007434 9e-122 2- 6: 10026606 10007432 2e-162 2- 7: 10026607 100074270 2- 8: 10026608 100074260 2- 9: 10026612 100074170 2- 10: 10026613 10007415 8e-128 2- 11: 10026614 10007414 3e-64 2- 12: 10026615 100074090 2- 13: 10026616 100074060 2- 14: 10026617 10007403 1e-171 2- 15: 10026618 100074020 2- 16: 10026619 10007397 7e-18 ........
要求:如果Alignment后面少于20行,把整个的去掉
二、实现方法:
python代码:
#!/usr/bin/python sum = 0 sumdata = [] FD = open("/root/data.txt","r") line = FD.readline() while line: if line.find("Alignment") == 3: if sum >= 20: for i in sumdata: print i, sum=0 sumdata=[line] else: sum = sum + 1 sumdata.append(line) line=FD.readline() if len(line) == 0: if sum >= 20: for i in sumdata: print i,
附:
perl代码
#!/usr/bin/perl open(FD,"/root/data.txt"); while (){ if ($_ =~ /Alignment/){ if($sum >= 20){ print @sumdata;} $sum=0; @sumdata=($_);} else{ $sum++; push(@sumdata,$_);} } print @sumdata if $sum >=20; close(FD);
- 看不过瘾?点击下面链接!
- php验证码生成器代码
- java如何利用java.net.URLConnection发送HTTP请求
- IOS中UIImageView方法实现简单动画
- Angular2 之 路由与导航教程
- Java网络通信基础编程
- 如何用node编写自己的cli工具