Python:读取上一行并与当前行进行比较

时间:2012-01-04 01:41:23

标签: python string for-loop

Python noob在Windows上使用2.7。我正在以编程方式在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_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:

我做错了什么,如何解决这个问题?我不知道这是否是字符串vs int比较,但如果是,我会感到愚蠢。我已经尝试了但是在检查最后一个“上一个:”时它会引发错误。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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循环
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()

答案 3 :(得分: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是文件对象。希望它有所帮助!