2017-03-21 10 views
-1

if line and not line[0].isdigit() and line != '\n':のコードは、プロジェクトの中と外で動作が異なるのはなぜですか? 私はライン88での私のプロジェクトmeltsubtitlesから次のコード抜粋があります:私はプロジェクトからコードを抽出し、text.txtでそれを実行したときにプリントアウトし、Pythonの `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 while文にそれが実行それはではありませんが、コードを抽出すると、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の中に88 meltsubtitlesラインです私は、Python 3.5を実行しているし、勝利10

+0

なぜdownvote? – EvanL00

答えて

0

によ同様コードについてのあなたの手段は何ですか?

私はコードでタスクを書き換える:

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() 
関連する問題