2012-01-04 16 views
0

PythonのnoobはWindowsで2.7を使用しています。私はプログラム的にHTMLで階層ツリービューを作ることに取り組んでいます。私はこれに似て出力されたファイルを持っている:私は遭遇してる問題があるということPython:前の行を読み込んで現在の行と比較します

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() 

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

マイコード:

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

out.txtを次のようになります私がprev_linecurrent_lineを比較しようとするたびに、私は希望の出力を得られません。私はif prev_line == "0": print "Root"のような何かをすることができますが、それはそのような場合だけで動作しません。また、次の電流が行われる前に部分が計算されているようです。現在のコードから得られる結果には、次のものが含まれます:

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

+0

@serk新しい質問であるはずです。 – Jordan

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

+0

"for"サイクルまたはファイルからのreadlineで次の反復のみが行われますが、コードの可読性が低下します。文字列のリストでファイル全体を読むことができます。 –

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'ですが、前の行はありません。 'Root'を出力するはずです。私の仮定が間違っている場合は、prev_line = '0'行をprev_line = ''に変更してください。

なぜ、ネストされたforループを実行し、次の行の値を取得しないのですか?コードの開始時にnum = 1と宣言してから、ネストされたforループを使用してruunします。

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

0

あなたは、私が作成したこの機能を使用することができます。self.handleはファイルオブジェクトである

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] 

。それが役に立てば幸い!

関連する問題