Remove blank lines from the bottom of a file when using while loop to check each line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peter9588
    New Member
    • May 2010
    • 2

    #1

    Remove blank lines from the bottom of a file when using while loop to check each line

    I am trying to read in every line from a given file and check (amongst other things) that the 3rd character of every line is a ';' (a semi colon). The files i make to go into my database needs to have every line with a two letter identifier followed by a semi colon.

    The issue I am having is that the while loop goes through every line fine and does examine the semi colon correctly, but always reports that there is a problem with the last line of the file, because it is whitespace...

    I have tried stripping lines, and getting the file loop to run only when the line has characters in it... but this seems to disable the while loop from working at all.

    See code Below.

    Code:
    # takes in each individual line of the file	
    line = fileIN.readline()	 
    
    # counter
    x = 1
    
    # while loop
    while line:
    # != None: 
    # != re.sub("\n\s*\n*", "\n", line):
        
    	line = fileIN.readline()
    	
    	line.strip()
    	
    	# checks that each line has a semi colon after each identifier
    	if line[2:3] != ";":
    		print "\nThere is a problem with the identifier on line", x + 1
    		print line
    Thanks in advance!
    Pete
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi Pete. Please use code tags when posting - it's very difficult to read otherwise. In fact your code makes me want to scream and run holding my head.

    That said, the task in hand should not be so very difficult. I normally try to engage in the code people give - if you want me to do so, please re-post it. But in the mean time, I would do it like this:
    Code:
    fileIn=open("whatever.txt")
    for i,line in enumerate(fileIn):
        if line.strip()=="": continue  #if the line is blank, go to the next line
        if line[2]!=";":
            print "It's going nuts on line",i+1
            print line
    fileIn.close()
    I haven't tested it, but it should work. I'm not 100% sure that enumerate works on a file object, but if you have any problems post back.

    In fact post back anyway, to let us know how it's going and what you get done.
    Last edited by Glenton; May 21 '10, 01:11 PM. Reason: forgot to close the file

    Comment

    • Peter9588
      New Member
      • May 2010
      • 2

      #3
      Originally posted by Glenton
      Hi Pete. Please use code tags when posting - it's very difficult to read otherwise. In fact your code makes me want to scream and run holding my head.

      That said, the task in hand should not be so very difficult. I normally try to engage in the code people give - if you want me to do so, please re-post it. But in the mean time, I would do it like this:
      Code:
      fileIn=open("whatever.txt")
      for i,line in enumerate(fileIn):
          if line.strip()=="": continue  #if the line is blank, go to the next line
          if line[2]!=";":
              print "It's going nuts on line",i+1
              print line
      fileIn.close()
      I haven't tested it, but it should work. I'm not 100% sure that enumerate works on a file object, but if you have any problems post back.

      In fact post back anyway, to let us know how it's going and what you get done.
      Thanks very much

      I manage to solve my problem, I think it was due to having two 'readin() lines'

      i removed one and it seemed to fix the problem.

      Comment

      Working...