473,499 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. # takes in each individual line of the file    
  2. line = fileIN.readline()     
  3.  
  4. # counter
  5. x = 1
  6.  
  7. # while loop
  8. while line:
  9. # != None: 
  10. # != re.sub("\n\s*\n*", "\n", line):
  11.  
  12.     line = fileIN.readline()
  13.  
  14.     line.strip()
  15.  
  16.     # checks that each line has a semi colon after each identifier
  17.     if line[2:3] != ";":
  18.         print "\nThere is a problem with the identifier on line", x + 1
  19.         print line
  20.  
Thanks in advance!
Pete
May 21 '10 #1
2 2304
Glenton
391 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. fileIn=open("whatever.txt")
  2. for i,line in enumerate(fileIn):
  3.     if line.strip()=="": continue  #if the line is blank, go to the next line
  4.     if line[2]!=";":
  5.         print "It's going nuts on line",i+1
  6.         print line
  7. 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.
May 21 '10 #2
Peter9588
2 New Member
@Glenton
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.
May 21 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8684
by: peter leonard | last post by:
Hi, I having a problem with reading each line from a text file. For example, the file is a text file named 'test.txt' with the following content : line 1 line 2 line 3 line 4 line 5
6
2287
by: Shen | last post by:
I saw this conclusion in a couple of groups. For example, if you define a class template, in VC++ you have to merge the .h and .cpp files into one file and include it in other files where they have...
2
15542
by: Sacha Korell | last post by:
How would I check for an end of file when parsing a text file using the StreamReader object? I would like to do something like this: '******************************** Dim objStreamReader As...
2
6491
by: James | last post by:
below is some codes. my arraylist below reads from a file. My files contains blank line (ie carriage return) My message dialog shows all strings being captured. However i do not want my...
3
3683
by: Jeff Calico | last post by:
Hello everyone I am transforming an XML document to text, basically only outputting a small portion of it. When I run the following XSLT via Xalan's processor, I get a bunch of unwanted blank...
1
3898
by: JR | last post by:
I have some code that cleans up an xml file before running xsl transformation on it due to what I call bad characters in the data. I have as an example: str = Replace(str, "", ".") str =...
0
1590
by: AboutJAV | last post by:
I am trying to add a crystal item through the add new option. I am using a dataset, which is loaded with the data and already has the .xsd in the project. I assigned the dataset with the report...
3
2609
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
2
2406
Patropia
by: Patropia | last post by:
Hi, I have an iframe that's using designMode in which I'm loading some code. When including an iframe in that code (putting an iframe in the iframe) the entire frame goes blank. This only happens...
7
7728
by: =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post by:
Hi, I am using StreamReader to read an ASCII file that contains blank lines. How can I omit reading blank lines? I tried somting like... FileStream inFile = new...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.