[英]how to read previous and next line if the current line is valid for an IF statement in Python
[英]Python: Read previous line and compare against current line
在Windows上使用2.7的Python noob。 我正在以編程方式在HTML中創建層次結構樹視圖。 我有一個輸出類似於此的文件:
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_line
與current_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:
我做錯了什么,如何解決這個問題? 我不知道這是否是字符串vs int比較,但如果是,我會感到愚蠢。 我已經嘗試了但是在檢查最后一個“上一個:”時它會引發錯誤。
將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
你可以使用我創建的這個功能:
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
是一個文件對象。 希望能幫助到你!
你的錯誤聲明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
將代碼更改為:
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循環。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.