473,799 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to remove empty lines with re?

ted
I'm having trouble using the re module to remove empty lines in a file.

Here's what I thought would work, but it doesn't:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line)
print line

Also, when I try to remove some HTML tags, I get even more empty lines:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub('<.*?>', '', line)
line = re.sub(r'^\s+$| \n', '', line)
print line

I don't know what I'm doing. Any help appreciated.

TIA,
Ted


Jul 18 '05 #1
9 23519
"ted" <te************ @yahoo.com> writes:
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line) # }
print line # }

If you will set a variable to an empty string and then print it, you will
get an empty line printed ;)

~Tim
--
Product Development Consultant
OpenLink Software
Tel: +44 (0) 20 8681 7701
Web: <http://www.openlinksw. com>
Universal Data Access & Data Integration Technology Providers
Jul 18 '05 #2
ted wrote:
I'm having trouble using the re module to remove empty lines in a file.

Here's what I thought would work, but it doesn't:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line)
print line


Try:

import sys
for line in f:
if line.strip():
sys.stdout.writ e(line)

Background: lines read from the file keep their trailing "\n", a second
newline is inserted by the print statement.
The strip() method creates a copy of the string with all leading/trailing
whitespace chars removed. All but the empty string evaluate to True in the
if statement.

Peter
Jul 18 '05 #3

"ted" <te************ @yahoo.com> wrote in message
news:vo******** ****@corp.super news.com...
I'm having trouble using the re module to remove empty lines in a file.

Here's what I thought would work, but it doesn't:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line)
print line


nonempty = [x for x in f if not x.strip()]

/BJ
Jul 18 '05 #4
To do this, you need to modify your re to just
this

empty=re.compil e('^$')

This of course looks for a pattern where there is beginning just
after end, ie the line is empty :-)

Here is the complete code.

import re

empty=re.compil e('^$')
for line in open('test.txt' ).readlines():
if empty.match(lin e):
continue
else:
print line,

The comma at the end of the print is to avoid printing another newline,
since the 'readlines()' method gives you the line with a '\n' at the end.

Also dont forget to compile your regexps for efficiency sake.

HTH

-Anand Pillai
"ted" <te************ @yahoo.com> wrote in message news:<vo******* *****@corp.supe rnews.com>...
I'm having trouble using the re module to remove empty lines in a file.

Here's what I thought would work, but it doesn't:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line)
print line

Also, when I try to remove some HTML tags, I get even more empty lines:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub('<.*?>', '', line)
line = re.sub(r'^\s+$| \n', '', line)
print line

I don't know what I'm doing. Any help appreciated.

TIA,
Ted

Jul 18 '05 #5
Errata:

I meant "there is end just after the beginning" of course.

-Anand

"ted" <te************ @yahoo.com> wrote in message news:<vo******* *****@corp.supe rnews.com>...
I'm having trouble using the re module to remove empty lines in a file.

Here's what I thought would work, but it doesn't:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line)
print line

Also, when I try to remove some HTML tags, I get even more empty lines:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub('<.*?>', '', line)
line = re.sub(r'^\s+$| \n', '', line)
print line

I don't know what I'm doing. Any help appreciated.

TIA,
Ted

Jul 18 '05 #6
Anand Pillai wrote:
Here is the complete code.

import re

empty=re.compil e('^$')
for line in open('test.txt' ).readlines():
if empty.match(lin e):
continue
else:
print line,
The .readlines() method retains any line terminators, and using the
builtin print will suffix an extra line terminator to every line,
thus effectively producing an empty line for every non-empty line.
You'd want to use e.g. sys.stdout.writ e() instead of print.
// Klaus

--<> unselfish actions pay back better

Jul 18 '05 #7
ted
Thanks Anand, works great.
"Anand Pillai" <py*******@Hotp op.com> wrote in message
news:84******** *************** ***@posting.goo gle.com...
To do this, you need to modify your re to just
this

empty=re.compil e('^$')

This of course looks for a pattern where there is beginning just
after end, ie the line is empty :-)

Here is the complete code.

import re

empty=re.compil e('^$')
for line in open('test.txt' ).readlines():
if empty.match(lin e):
continue
else:
print line,

The comma at the end of the print is to avoid printing another newline,
since the 'readlines()' method gives you the line with a '\n' at the end.

Also dont forget to compile your regexps for efficiency sake.

HTH

-Anand Pillai
"ted" <te************ @yahoo.com> wrote in message

news:<vo******* *****@corp.supe rnews.com>...
I'm having trouble using the re module to remove empty lines in a file.

Here's what I thought would work, but it doesn't:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub(r'^\s+$| \n', '', line)
print line

Also, when I try to remove some HTML tags, I get even more empty lines:

import re
f = open("old_site/index.html")
for line in f:
line = re.sub('<.*?>', '', line)
line = re.sub(r'^\s+$| \n', '', line)
print line

I don't know what I'm doing. Any help appreciated.

TIA,
Ted

Jul 18 '05 #8
You probably did not read my posting completely.

I have added a comma after the print statement and mentioned
a comment specifically on this.

The 'print line,' statement with a comma after it does not print
a newline which you also call as line terminator whereas
the 'print' without a comma at the end does just that.

No wonder python sometimes feels like high-level psuedocode ;-)
It has that ultra intuitive feel for most of its tricks.

In this case, the comma is usually put when you have more than
one item to print, and python puts a newline after all items.
So it very intuitively follows that just putting a comma will not
print a newline! It is better than telling the programmer to use
another print function to avoid newlines, which you find in many
other 'un-pythonic' languages.

-Anand

Klaus Alexander Seistrup <sp**@magneti c-ink.dk> wrote in message news:<3f******* *************** *************** ******@news.szn .dk>...
Anand Pillai wrote:
Here is the complete code.

import re

empty=re.compil e('^$')
for line in open('test.txt' ).readlines():
if empty.match(lin e):
continue
else:
print line,


The .readlines() method retains any line terminators, and using the
builtin print will suffix an extra line terminator to every line,
thus effectively producing an empty line for every non-empty line.
You'd want to use e.g. sys.stdout.writ e() instead of print.
// Klaus

Jul 18 '05 #9
Anand Pillai wrote:
You probably did not read my posting completely.

I have added a comma after the print statement and mentioned
a comment specifically on this.
You are completely right, I missed an important part of your posting.
I didn't know about the comma feature, so thanks for teaching me!

Cheers,

// Klaus

--<> unselfish actions pay back better

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8903
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
18
3070
by: Piotr Wolski | last post by:
i read the filr into an array using file(). some of the lines are empty. i would like to output the file but ignoring the emnpty lines. any ideas?
8
2865
by: JT | last post by:
i have written some asp that reads a fixed length text file, line by line, inserting each line into my database. my problem is that the text file format seems to have extra space at the end of the file so my code thinks there is actually one more line when there is not. is there a way to remove these spaces before i loop through the file? here is my sample code: Do While df.AtEndOfStream <> True line = df.ReadLine
2
3996
by: Olveres | last post by:
Hi, I have managed to work out how to add new lines into a calculated text box. However in this text box some of the outcome fields are empty however when previewing the report it includes the blank fields, so each section of the report is the same size, my field is set to can grow/shrink, but I think my inclusion in the code for the calculated box of all 15 outcomes (I have no choice) is what's causing each calculated box to be the...
5
31049
by: LEM | last post by:
Hi, I'm trying to remove any empty lines from a string, and I am doing the following: String pp; pp = "\r\n\r\n1\r\n23\r\n\r\n4"; pp = pp.Replace("\r\n\r\n", "\r\n");
1
10536
by: SM | last post by:
Hello, Im creating a combobox using the Javascript and the DOM. If the combobox exists, i don't want to create another one. I want to empty all the values and put new values. I was able to create the combox and create the lines of code that deletes the values in the combobox (well almost) I just need help finishing my function
4
22355
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside the string. I am using something like the following: var txt = textarea_element.value.replace(/\n*$/, ''); But this replaced only the last newline(by changing '' to 'K', and
3
5278
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
6
4257
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also this program has more then one class and also uses an interface. Right now I am working on ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.