2012-01-04 186 views
0

在Windows上使用2.7的Python noob。我正在以編程方式在HTML中創建層級樹視圖。我有一個輸出類似於這樣一個文件:Python:讀取上一行並與當前行進行比較

0 
    2 
    4 
     6 
     8 
     8 
0 
    2 
    4 
    4 
     6 
     8 
     8 
     6 
     6 

out.txt看起來是這樣的:

0 
2 
4 
6 
8 
8 
0 
2 
4 
4 
4 
6 
8 
8 
6 
6 

我的代碼:

x = open('out.txt','r') 

for current_line in x: 
    prev_line = current_line[:-1] 
    print "Current: " + current_line 
    print "Previous: " + prev_line 
    if prev_line > current_line: 
     print "Folder" 
    elif prev_line == current_line: 
     print "Root" 

x.close() 

我現在遇到的問題是,每次我嘗試將prev_linecurrent_line比較時,我都沒有得到所需的輸出。我可以做類似if prev_line == "0": print "Root"的事情,但那不會起作用,因爲它僅適用於這種情況。此外,似乎在下一個電流完成之前正在計算if部分。我從當前代碼得到的結果包括:

Current: 0 

Previous: 0 
Current: 2 

Previous: 2 
Current: 4 

Previous: 4 
Current: 6 

Previous: 6 
Current: 8 

Previous: 8 
Current: 8 

Previous: 8 
Current: 0 

Previous: 0 
Current: 2 

Previous: 2 
Current: 4 

Previous: 4 
Current: 4 

Previous: 4 
Current: 4 

Previous: 4 
Current: 6 

Previous: 6 
Current: 8 

Previous: 8 
Current: 8 

Previous: 8 
Current: 6 

Previous: 6 
Current: 6 
Previous: 

我在做什麼錯,我該如何解決這個問題?我不知道這是一個字符串還是int比較,但如果是這樣的話,我會感覺到愚蠢。我試過了,但是在檢查最後一個「Previous:」時會引發錯誤。

回答

2

將prev_line設置爲for循環結尾處的current_line值。

prev_line = '' 

for current_line in x: 
    print "Current: " + current_line 
    print "Previous: " + prev_line 

    if prev_line > current_line: 
     print "Folder" 
    elif prev_line == current_line: 
     print "Root" 

    prev_line = current_line 
+0

有什麼辦法可以得到下一行?例如,我想查看下一封信的內容。我將如何實現這一點? – serk 2012-01-05 00:46:06

+0

@serk應該是一個新問題。 – Jordan 2012-01-06 03:22:58

0

您錯誤的聲明prev_line。如果current_line讀爲'0 \ n',則prev_line爲'0'。 而prev_line絕不會大於current_line。 嘗試聲明prev_line爲全行。

prev_line = '0' 

for current_line in x: 
    print "Current: " + current_line 
    print "Previous: " + prev_line 
    if prev_line > current_line: 
     print "Folder" 
    elif prev_line == current_line: 
     print "Root" 
    prev_line = current_line 
+0

有什麼方法可以獲得下一行?例如,我想查看下一封信的內容。我將如何實現這一點? – serk 2012-01-05 00:41:10

+0

只有在「for」循環或readline從文件中進行下一次迭代,但會降低代碼的可讀性。您可以在字符串列表中讀取整個文件。 – 2012-01-05 09:26:45

0

更改代碼:

x = open('out.txt','r') 
prev_line='0' 
for current_line in x:  
print "Current: " + current_line  
if prev_line != '': 
    print "Previous: " + prev_line  
if prev_line > current_line:   
    print "Folder"  
elif prev_line == current_line:   
    print "Root" 
prev_line=current_line 
x.close() 

這應該解決的問題。對於第一行,當前行是'0',但沒有以前的行..所以我想它應該打印'根'。如果我的假設是錯誤的,則將prev_line ='0'行更改爲prev_line =''。

爲什麼不運行嵌套for循環並獲取下一行的值。比如在代碼的開頭聲明num = 1,然後使用一個嵌套for循環。

x = open('out.txt','r') 
prev_line='0' 
num=1 
for current_line in x: 
y=1 
for next_line in x: 
    if y=num+1: 
    nextline=next_line 
    y+=1 
print "Next Line "+next_line 
num+=1 
x.close() 
+0

有什麼方法可以獲得下一行?例如,我想查看下一封信的內容。我將如何實現這一點? – serk 2012-01-05 00:45:53

0

您可以使用此功能,我創建:

def before(self): 
    # get the current cursor position, then 
    # store it in memory. 
    current_position = self.tell() 

    # move back by 1 byte at a time and store 
    # that byte's contents into memory. 
    _tmp = "" 
    _rea = 0 
    if "b" in self.mode: 
     _tmp = b"" 

    while True: 
     _rea += 1 
     # move back x bits and read one byte 
     self.handle.seek(current_position - _rea) 

     # now we've read one byte 
     _tmp += self.handle.read(1) 

     # move back one byte again 
     self.handle.seek(current_position - _rea) 

     # If the string in memory contains the "\n" 
     # EOL, we will return the string. 

     # alternatively break if the current position 
     # is zero. 
     if _tmp.count("\n") == 2 or self.tell() == 0: 
      break 

    # somehow the string is reversed... 
    return _tmp[::-1] 

self.handle是一個文件對象。希望能幫助到你!

相關問題