2014-07-11 48 views
-3

解析我試圖打印具有單詞一個程序,我得到它具有分裂和印刷下一字行:我的計劃是沒有得到輸出Python從動態輸入線

with open("qwer.txt",'r') as v1: 
    for line in v1: 
    if 'good' in v1: 
     print(v1) 
    if '=' in v1:  
     print v1.split('=')[1] 

    if '==' in v1: 
     print v1.split('==')[1] 

qwer.txt:

Ram is very good=ideal student 
Ram has nice character==Perfect student 

輸出:

Ram is very good=ideal student 
ideal 

它一旦語句p不執行rovided,請幫我得到輸出!

+0

你的問題到底是什麼?該代碼完全按照您的要求進行操作 - 選擇具有最高「fuzz.ratio」(不管那是什麼)的行並打印在「=」後面的行。請提供一個[minimal example](http://stackoverflow.com/help/mcve),其他人可以使用它來複制行爲(即包括'fuzz.ratio'),並清楚地解釋你所得到的與你的不同之處預期。 – jonrsharpe

+0

我編輯了這個問題,你能幫我讓輸出運行嗎? – adarshram

+0

你的新「代碼」不會運行(''NameError''如果'好'在線),你仍然沒有解釋問題是什麼**。 – jonrsharpe

回答

0

您正在測試'=='in v1,不是line。嘗試:

with open("qwer.txt",'r') as v1: 
    for line in v1: 
    if 'good' in line: # note 'line' from here on in 
     print(line) 
    if '=' in line:  
     print line.split('=')[1] 
    if '==' in line: 
     print line.split('==')[1] 

這給了我:

Ram is very good=ideal student 

ideal student 


Perfect student 

你也應該記住,你的'='條件並不是相互排斥的;如果'=='line那麼'='也是如此。它可能會更好,如:

with open("qwer.txt",'r') as v1: 
    for line in v1: 
    if 'good' in line: # note 'line' from here on 
     print(line) 
    if '==' in line: # '==' first 
     print line.split('==')[1] 
    elif '=' in line: # 'elif', not just 'if' 
     print line.split('=')[1]