2017-03-21 16 views
-1

코드 if line and not line[0].isdigit() and line != '\n':이 프로젝트 안팎에 대해 왜 다르게 동작합니까? 나는이 라인 (88)의 내 프로젝트 meltsubtitles에서 다음 코드 추출물 : 나는이 프로젝트에서 코드를 추출하고 text.txt 그것을 실행할 때파이썬의`if` 문은 어디서 실행해야 하는가에 따라 다르게 동작합니까?

with open('test.txt', 'r', encoding='utf-8') as finput: 
    for line in finput: 
     if line and not line[0].isdigit() and line != '\n': 
      pass 
     else: 
      print(line) 

, 인쇄 :

1 

00:00:03,940 --> 00:00:07,550 



2 

00:00:09,280 --> 00:00:10,650 

을하지만 때 내가 내 프로젝트에 비슷한 코드를 넣으면 첫 번째 line '1\n'이 인쇄되지 않습니다. 출력은 다음과 같습니다

00:00:03,940 --> 00:00:07,550 

2 
00:00:09,280 --> 00:00:10,650 

내가 기대하는 것은 : 내가 디버그 pycharm을 사용하고 중요한 라인 if line and not line[0].isdigit() and line != '\n': 한 단계 한 line = '1\n'

1 
00:00:03,940 --> 00:00:07,550 

2 
00:00:09,280 --> 00:00:10,650 

if 문 반면에이 이상하게 실행 ,, 코드를 추출하면 if 문으로 실행되지 않습니다.

test.txt 파일

1 
00:00:03,940 --> 00:00:07,550 
Horsin' Around is filmed before a live studio audience. 

2 
00:00:09,280 --> 00:00:10,650 
Mondays. 

내 프로젝트는 GitHub의 meltsubtitles 라인 88 에서입니다 내가 파이썬 3.5

+0

왜 downvote을? – EvanL00

답변

0

유사 코드에 대한 귀하의 수단 무엇 승리 10에서 실행 해요?

나는 코드로 작업을 재 작성 :

import logging 
logging.basicConfig(level=logging.DEBUG) 

def begin_numebr(string=None): 
    if string is None: 
     return False 
    else: 
     return string.strip() != '' and string[0].isdigit() 

def line_filter(lines): 
    return filter(lambda line: begin_numebr(line), lines) 

for line in line_filter(open('test.txt')): 
    print(line) 

def test(): 
    assert(begin_numebr('')==False) 
    assert(begin_numebr(' ')==False) 
    assert(begin_numebr('\n')==False) 
    assert(begin_numebr('\t')==False) 
    assert(begin_numebr('\b')==False) 
    assert(begin_numebr()==False) 
    assert(begin_numebr('not digit')==False) 
    assert(begin_numebr('00not digit')==True) 
    print('test done') 

    lines = ['001', 'this is'] 
    logging.debug(line_filter(lines)) 
    assert(line_filter(lines) == ['001']) 

#test()