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

Python:阅读前一行并与当前行进行比较,python,string,for-loop,Python,String,For Loop,PythonNoob在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') f

PythonNoob在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_line==“0”,我可以执行类似于
的操作:打印“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:”.时会出现错误。

将prev\u行设置为for循环末尾的当前行的值

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\n”,则上一行为“0”。 上一行永远不会大于当前行。 尝试将上一行声明为整行

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,然后使用ruun嵌套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()

您可以使用我创建的此函数:

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
是一个文件对象。希望有帮助

有没有办法到下一条线?例如,我想检查下一封信是什么。我该如何实现这一点?只需在“for”循环中的下一次迭代或从文件中读取,但它会降低代码的可读性。您可以在字符串列表中读取整个文件。是否有方法获取下一行?例如,我想检查下一封信是什么。我将如何实现这一点?有没有办法获得下一行?例如,我想检查下一封信是什么。我将如何实现这一点?@serk这应该是一个新问题。