Search code examples
pythonfilelines

Python - Check Order of Lines in File


How does one check the order of lines in a file?

Example file:

a b c d e f
b c d e f g
1 2 3 4 5 0

Requirements:

  1. All lines beginning a, must precede lines beginning b.
  2. There is no limit on number of lines beginning a.
  3. Lines beginning a, may or may not be present.
  4. Lines containing integers, must follow lines beginning b.
  5. Numeric lines must have at least two integers followed by zero.
  6. Failure to meet conditions must raise error.

I initially thought a rather long-winded for loop, but that failed as I am unable to index lines beyond line[0]. Also, I do not know how to define location of one line relative to the others. There is no limit on the length of these files so memory may also be an issue.

Any suggestions very welcome! Simple and readable is welcome for this confused novice!

Thanks, Seafoid.


Solution

  • A straightforward iterative method. This defines a function to determine a linetype from 1 to 3. Then we iterate over the lines in the file. An unknown line type or a linetype less than any previous one will raise an exception.

    def linetype(line):
        if line.startswith("a"):
            return 1
        if line.startswith("b"):
            return 2
        try:
            parts = [int(x) for x in line.split()]
            if len(parts) >=3 and parts[-1] == 0:
                return 3
        except:
            pass
        raise Exception("Unknown Line Type")
    
    maxtype = 0
    
    for line in open("filename","r"):  #iterate over each line in the file
        line = line.strip() # strip any whitespace
        if line == "":      # if we're left with a blank line
            continue        # continue to the next iteration
    
        lt = linetype(line) # get the line type of the line
                            # or raise an exception if unknown type
        if lt >= maxtype:   # as long as our type is increasing
            maxtype = lt    # note the current type
        else:               # otherwise line type decreased
            raise Exception("Out of Order")  # so raise exception
    
    print "Validates"  # if we made it here, we validated