serk
serk

Reputation: 4389

Python: Read previous line and compare against current line

Python noob using 2.7 on Windows. I'm working on making a hierarchy tree view in HTML programatically. I've got a file that is outputted similar to this:

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

out.txt looks like this:

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

My code:

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

The problem I'm encountering is that each time I try to compare the prev_line to the current_line I do not get the desired output. I can do something like if prev_line == "0": print "Root" but that will not work as it is for only that case. Also, it seems like the if portion is being calculated before the next current is done. The results that I get from my current code includes:

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:

What am I doing wrong and how do I fix this? I don't know if this is a string vs int comparison but if it is I'm gonna feel dumb. I've tried it but it raises an error when checking the last "Previous:".

Upvotes: 0

Views: 9821

Answers (4)

Eugene
Eugene

Reputation: 161

You can use this function I created:

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]

Where self.handle is a file object. Hope it helps!

Upvotes: 0

Arnab Ghosal
Arnab Ghosal

Reputation: 521

Change the code as:

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

That should solve the issue. For the first line the current line is '0' but there is no previous line..so I guess it should print 'Root'. If my assumption is wrong then change prev_line='0' line to prev_line=''.

why not run a nested for loop and get the value of the next line. like declare num=1 at the start of the code and then ruun a nested for loop using.

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

Upvotes: 0

Valentin Briukhanov
Valentin Briukhanov

Reputation: 1283

Your wrong declare prev_line. If current_line read as '0\n' so prev_line is '0'. And prev_line never greater than current_line. Try declare prev_line as full 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

Upvotes: 0

Jordan
Jordan

Reputation: 32522

Set prev_line to the value of current_line at the end of the for loop.

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

Upvotes: 2

Related Questions