2014-09-26 5 views
0

그래서 파일의 섹션 만 인쇄하는 조건을 사용하려고했지만 ipython에서 코드를 실행할 때 어떤 이유로 연속적으로 실행되고 멈추지 않습니다.파이썬에서 파일의 일부만 사용하는 방법은 무엇입니까?

Use the -noinfo option to turn off this help. 
Use the -help option to get a list of command line options. 

pilercr v1.06 
By Robert C. Edgar 

Temp1.None.fasta: 523 putative CRISPR arrays found. 



DETAIL REPORT 



Array 1 
>contig-856000000 902 nucleotides 

     Pos Repeat  %id Spacer Left flank Repeat          Spacer 
========== ====== ====== ====== ========== ======================================== ====== 
     28  40 95.0  26 TGCTTCCCCG -.....................................T. CTTGGTCTTGCTGGTTCTCACCGACT 
     94  40 95.0  25 CTCACCGACT .T....................................C. GTCAGCGTGTAGCGACTGTATCTGG 
     159  40 100.0   CTGTATCTGG ........................................ TTGCTCGAA 
========== ====== ====== ====== ========== ======================================== 
     3  40    25    TAGTTGTGAATAGCTGACAAAATCATATCATATACAACAG 


Array 2 
>contig-2277000000 590 nucleotides 

     Pos Repeat  %id Spacer Left flank Repeat         Spacer 
========== ====== ====== ====== ========== ===================================== ====== 
     19  37 100.0  37 GAGGGTGAGG ..................................... ACTTTAGGTTCAAATCCGTAGAGCTGATCTGTAATAG 
     93  37 100.0  37 TCTGTAATAG ..................................... ATTCCGTTGTTGAAATAAAGTATGAATAATATTTGGT 
     167  37 100.0  35 AATATTTGGT ..................................... TTCTCGAACGTTCCATGCTTCATAATATACCTCCT 
     239  37 100.0  39 TATACCTCCT ..................................... CTGATGAATCTTACCTCGTACAGTGATGTAGCCAGGTAA 
     315  37 100.0   AGCCAGGTAA ..................................... CGTCAGTCATG 
========== ====== ====== ====== ========== ===================================== 
     5  37    37    GTAGAAATGAGACGTCCGCTGTAAAGGACATTGATAC 


Array 3 
>contig-2766000000 540 nucleotides 

     Pos Repeat  %id Spacer Left flank Repeat         Spacer 
========== ====== ====== ====== ========== ===================================== ====== 
     172  37 100.0  29 GTTTTAGATG ..................................... TATCGTAGCATCCCACTCCCCTGGTGTAA 
     238  37 100.0  29 CCTGGTGTAA ..................................... GTTGGACGCGCTGCTGGACGATAGGCTGC 
     304  37 97.3  29 GATAGGCTGC T.................................... ACGCCTTACAAGCTGACCCGCGCCCAATT 
     370  37 100.0   GCGCCCAATT ..................................... GTACCTTGTTC 
========== ====== ====== ====== ========== ===================================== 
     4  37    29    GGCTGTAAAAAGCCACCAAAATGATGGTAATTACAAG 


SUMMARY BY SIMILARITY 



Array   Sequence Position  Length # Copies Repeat Spacer + Consensus 
===== ================ ========== ========== ======== ====== ====== = ========= 
    5 contig-504300000   18   364   6  33  33 + --------------------------GTCGCT-C---CCCGCATGGGGAGCG--T-GGATTGAAAT----- 
    8 contig-974700000   15   229   4  32  33 - --------------------------GTCGCC-C---CCCATGCG-GGGGCG--T-GGATTGAAAC----- 
    12 contig-759000001   464   503   8  33  34 + --------------------------GTCGCT-C---CCTTTACGGGGAGCG--T-GGATTGAAAT----- 
    16 contig-293000000   77   406   6  37  36 - -----------------------GTAGAAATGAG---TTCCCCGATGAGAAG--G-GGATTGACAC----- 
    17 contig-457600000   28   416   6  37  38 - -----------------------GTAGAAATGGG---TGTCCCGATAGATAG--G-GGATTGACAC----- 
    18 contig-527300000   1   351   6  33  32 + -----------------------ATCGCG----C---CCCCACGGGGGCGTG--T-GAATTGAAAC----- 
    27 contig-132220000   21   234   4  33  34 + --------------------------GTCGCT-C---CCTTCACGGGGAGCG--T-GGATTGAAAT----- 
    36 contig-602400000   35   304   5  33  34 - --------------------------GTCGCC-C---CCCACGTGGGGGGCG--T-GGATTGAAAC----- 
    38 contig-124860000   131   232   4  32  34 + --------------------------GTCGCA-C---CCCTCGC-GGGTGCG--T-GGATTGAAAC----- 
    54 contig-979400000   138   231   4  32  34 - --------------------------GTCGCC-C---CTCTTGCA-GGGGCG--T-GGATTGAAAC----- 
    61 contig-992000005   149   693  11  30  36 - --------------------GTTAAAATCA--GA---CC---ATTTTG--------GGATTGAAAT----- 
    68 contig-103110000   37   238   4  34  34 + -----------------------GTCGTC----C---CCCACACGGGGGACG--T-GGATTGAAATA---- 
    73 contig-372900000  1627  1013  16  30  35 + ----------------------------ATTAGAATCGTACTT--ATGTAGAATTGAAAT----------- 

그리고 내 코드는 지금까지 있습니다 :

I가 그것을 실행하고 파일은

fname = 'crispr_pilrcr_1.out' 
start=False 
end=False 
counter = 0 
for line in open(fname, 'r'): # Open up the file 
    s = line.split() # Split each line into words 
    if not s: continue # Remove empty lines which would otherwise cause errors 
    if '==' in s[0]: continue # Removes seperation lines which consist of long '=======' strings 
    try: 
     if s[0] == 'DETAIL': # Only start in the section which starts with 'DETAIL' 
      start=True 
      print 'Starting' 
     if s[0] == 'SUMMARY': # Only end once this section has ended 
      end=True 
      print 'Ending' 
     while start==True or end==False: # Whilst in the section of the PILER-CR output which provides spacer sequences 
      try: 
       int(s[0]) 
       print s[7] 
      except ValueError: 
       continue 
    except ValueError: 
     continue 

내가 루프 '동안'그러나 같은 문제 가능성이 뭔가가 파악 'or'대신 'and'를 사용하면 연속 실행이 발생합니다.

필자는 'DETAIL REPORT'와 'SUMMARY BY SIMILARITY'사이에서 파일의 일부를 선택하고 싶습니다. 따라서 필자는 일단 조건이 발견되면 시도 할 조건을 설정합니다.

여러분이 제공 할 수있는 도움이 될 것입니다.

감사합니다, 톰

+1

나는 어둠 속에서 총을 쏜다. 여기. 'while'을'if'로 바꾸어보세요. (그리고 아마도 '또는'with '와') – Kevin

답변

3

고려 뭔가 기본적으로

fname = 'crispr_pilrcr_1.out' 
counter = 0 
printing = False 
for line in open(fname, 'r'): # Open up the file 
    s = line.split() # Split each line into words 
    if not s: continue # Remove empty lines which would otherwise cause errors 
    if '==' in s[0]: continue # Removes seperation lines which consist of long '=======' strings 
    try: 
     if s[0] == 'DETAIL': # Only start in the section which starts with 'DETAIL' 
      printing = True 
      print 'Starting' 
     elif s[0] == 'SUMMARY': # Only end once this section has ended 
      printing = False 
      print 'Ending' 
     elif printing: 
      try: 
       # Anything you put here will only be called for the lines 
       # between DETAIL... and SUMMARY... 
      except ValueError: 
       continue 
    except ValueError: 
     continue 

등을 단일 변수 printing를 사용하는 False로 초기화되고 for 루프가 "DETAIL ..."을 만나면 True로 설정되고 for 루프가 "SUMMARY ..."를 만나면 False로 재설정됩니다.

"DETAIL ..."또는 "SUMMARY ..."와 일치하지 않는 라인과 printing이 참이면 (즉, 두 제목 사이의 라인의 경우) try 블록이 실행됩니다.

+0

완벽하게 작동합니다. 정말 고맙습니다! – Tom

+0

마지막'elif' 안에'try' 블록이 필요없는 것 같습니다. –

1

이 문제는 당신이 당신의 while 루프 내부 start 또는 end의 값을 변경하지 않을 것입니다. 따라서 루프에 들어갈 수있는 값은 무엇이든간에 모든 반복에서 동일합니다.

완전히 논리를 정밀 검사하지 않고, 당신이 아마 같은 것을 할 것인지 추측에는 요 :

while start or not end: 
    try: 
     int(s[0]) 
     print s[7] 
    except ValueError: 
     end = True 
     start = False 
+0

'ValueError'가 없다면 당신은 여전히 ​​무한 루프를 가지고 있습니다. –

+0

감사합니다.이 무한 루프를 해결할 수 있지만 여전히 인쇄하지 않습니다. – Tom

관련 문제