2013-11-20 142 views
0
all_courses=open("E:/we/aa.txt","r").readlines() 
completed_ones=open("E:/we/aaa.txt","r") 
read_completed_ones=completed_ones.readlines() 
my_dict={} 
my_dict["completed courses"]=read_completed_ones 

for line in all_courses: 
    if line.strip().startswith("#"): 
    continue 
    splitted=line.split(",") 
    #print splitted 
    courses_remaining=splitted[2] 
    #print courses_remaining 
    for every in read_completed_ones: 
    the_big=every 
    if courses_remaining==the_big: 
     print courses_remaining 

我的問題是,如果courses_remaining == the_big,the_big是最後一行,我不知道如何使所有行不只是最後一行。感謝幫助。python只讀最後一行

+0

您的代碼很難讀取,只有2個縮進空格。 ''__b'被設置爲'read_completed_ones'中的每一行,而不僅僅是最後一行,除非你在後面的'if'語句的縮進中做了錯誤。 –

+0

這聽起來好像'如果courses_remaining == the_big'縮進在你的* actual *代碼中是不正確的;確保它縮進以匹配'the_big = every'行。如果你正在混合標籤和空格,不要,那隻會導致更多像這樣的問題。 –

+0

問題是我確定選項卡是正確的,但是當我打印之前如果courses_remaining == the_big打印,它會打印所有行,但如果縮進後它只打印最後一行@MartijnPieters – Ankosh

回答

1

你需要確保壓痕是正確的:

for every in read_completed_ones: 
    the_big=every 
    if courses_remaining==the_big: 
     print courses_remaining 

是正確的,但在你的情況下,縮進是真的:

for every in read_completed_ones: 
    the_big=every 
if courses_remaining==the_big: 
    print courses_remaining 

這意味着the_big現指最後一行。

您可能正在混合標籤和空格;不要這樣做。使用python -tt運行腳本以檢測任何不一致的Tab鍵,並用編輯器中的空格替換所有的Tab鍵。