2012-07-06 27 views
0

我想寫一個Python腳本,使用屬於我工作的公司的特定外部應用程序。在編程和腳本編寫方面,我通常可以爲自己弄清楚這些事情,但是這次我真的很失落!雖然循環不執行主要功能

我似乎無法弄清楚爲什麼while循環不會正常工作。它不會給出任何不幫助我的錯誤。它似乎跳過循環中心的代碼的重要部分,然後繼續增加「計數」,就像之後應該那樣!

f = open('C:/tmp/tmp1.txt', 'w') #Create a tempory textfile 
f.write("TEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\n") #Put some simple text in there 
f.close() #Close the file 

count = 0 #Insert the line number from the text file you want to begin with (first line starts with 0) 
num_lines = sum(1 for line1 in open('C:/tmp/tmp1.txt')) #Get the number of lines from the textfile 

f = open('C:/tmp/tmp2.txt', 'w') #Create a new textfile 
f.close() #Close it 

while (count < num_lines): #Keep the loop within the starting line and total number of lines from the first text file 
    with open('C:/tmp/tmp1.txt', 'r') as f: #Open the first textfile 
     line2 = f.readlines() #Read these lines for later input 
     for line2[count] in f: #For each line from chosen starting line until last line from first text file,... 
      with open('C:/tmp/tmp2.txt', 'a') as g: #...with the second textfile open for appending strings,... 
       g.write("hello\n") #...write 'hello\n' each time while "count" < "num_lines" 
    count = count + 1 #Increment the "count" 

我覺得一切正常,直到:「對於F中的2號線[計數]:」

我工作的實際代碼稍微複雜一些,並且應用程序我使用ISN」 t完全是爲了共享,所以我簡化了代碼來給愚蠢的輸出,而不是爲了解決問題。

我不在尋找替代代碼,我只是尋找一個爲什麼循環不工作的原因,所以我可以嘗試自己修復它。

所有的答案將不勝感激,並感謝大家提前!

科馬克

+1

你可以使用'for line [count] in f:'?你有沒有嘗試'在f:'行? – Wug 2012-07-06 14:32:49

+1

呵呵。你的問題是''read'()'調用後'f'沒有更多的行要讀,所以它在'line2 [count]'中放置了一個'None'(關於之前的內容)。 – cha0site 2012-07-06 14:33:30

+1

它可能與在f中使用'line2 [count]有關。如果你創建了line2,你應該在line2中使用'for line:'。 – purpleladydragons 2012-07-06 14:35:47

回答

2

一些評論:

num_lines = sum(1 for line1 in open('C:/tmp/tmp1.txt')) 

爲什麼? len(open(filename, 'rb').readlines())有什麼不對?

while (count < num_lines): 
    ... 
    count = count + 1 

這是壞的風格,你可以使用:

for i in range(num_lines): 
    ... 

請注意,我命名你的索引i,這是舉世公認的,而且我用rangefor循環。

現在,您的問題,就像我在評論中所說的那樣,f是一個文件(即帶有位置指針的字節流),並且您已經讀取了它的所有行。所以當你做for line2[count] in f:時,它會嘗試讀取一行到line2[count](這有點奇怪,實際上,你幾乎從不使用for循環,列表成員作爲索引,但顯然你可以這樣做),看到沒有行閱讀,永遠不會執行循環內部的內容。

無論如何,你想從一個給定的行號開始逐行讀取一個文件?這裏有一個更好的方式來做到這一點:我知道你不想替代碼

from itertools import islice 

start_line = 0 # change this 
filename = "foobar" # also this 

with open(filename, 'rb') as f: 
    for line in islice(f, start_line, None): 
     print(line) 

,但你的代碼確實是不必要的複雜。

+0

感謝您的回答!我之所以沒有選擇其他代碼,是因爲我喜歡以艱難的方式學習東西。這樣,我知道我會更好地理解它,當我自己找出某些東西時,我可能會記得如何永久再做一次。但我喜歡你給我的「小」選擇,而不是一個很大的選擇。這樣我可以花時間進一步調查。謝謝! – FCormacB 2012-07-06 15:05:13

+0

'rb'是否以二進制格式讀取文件?這是否會提高代碼效率? – FCormacB 2012-07-06 15:15:33

+0

@FCormacB:'b'實際上是二進制的簡寫,但是在FTP'BINARY'和'ASCII'命令的意義上。沒有它,在讀取和寫入文件時,Windows上的Python會在'\ r \ n'和'\ n'之間進行轉換。我通常不喜歡我的文件被隱式修改,因此在打開文件時使用'b'。短版本 - 它對效率沒有任何作用,但對於二進制文件(JPEG,MP3,...),正確性非常重要。 – cha0site 2012-07-06 15:21:30

0

如果要遍歷在文件f行,我建議用

for line in line2: 
    # do something with "line"... 

你把線稱爲2號線陣列更換你「爲」行,所以在使用那個陣列!使用line2 [count]作爲循環變量對我來說沒有意義。

+0

好的。謝謝!我只是改變了上面的代碼,現在我認爲它現在按照它的意思工作。現在我必須看看我能否修復真實的代碼。再次感謝大家! – FCormacB 2012-07-06 14:45:49

0

你好像錯了'for line in f'循環的工作原理。它迭代一個文件並調用readline,直到沒有行可讀。但是在你開始循環的時候,所有的行都已經被讀取(通過f.readlines()),並且文件的當前位置已經結束。你可以通過調用f.seek(0)來達到你想要的效果,但這似乎並不是一個好的決定,因爲你將再次讀取文件,這就是IO的緩慢。 相反,你想要做像水木清華:

for line in line2[count:]: # iterate over lines read, starting with `count` line 
    do_smth_with(line) 
+0

有趣。我會繞過我的想法。 – FCormacB 2012-07-06 15:06:19