473,372 Members | 1,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 23427
"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.write(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.supernews.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.compile('^$')

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.compile('^$')
for line in open('test.txt').readlines():
if empty.match(line):
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.supernews.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.supernews.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.compile('^$')
for line in open('test.txt').readlines():
if empty.match(line):
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.write() instead of print.
// Klaus

--<> unselfish actions pay back better

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

empty=re.compile('^$')

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.compile('^$')
for line in open('test.txt').readlines():
if empty.match(line):
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.supernews.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**@magnetic-ink.dk> wrote in message news:<3f****************************************** *@news.szn.dk>...
Anand Pillai wrote:
Here is the complete code.

import re

empty=re.compile('^$')
for line in open('test.txt').readlines():
if empty.match(line):
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.write() 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
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...
18
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
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...
2
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...
5
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
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...
4
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...
3
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...
6
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.