954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to delete blank lines in a file??

Hello everyone,

My file has lines like:

>9|102396631|genome.....

CCACTTTCTCTCCGCGCTGGGTTGAACATGGTACATCAGACTCCCTGAATCTGTCAGATC

TCTTGGTTGCTGTTGACAACTAAGACTTCGGAGCCTGGAG_GATTTGTTGAGGGGGAGCA

GTTCTGGGTGTCCTCTGCTGTGACTGGGCTCCCGGCCTCTTGATAATGCGCAACAGCTGC

GGTAGAACATCTCATTCCCAG

>9|102362951|ENSRNOS....

I want to delete blank lines from this file please help me...

Thanks

joe82
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Just read the file as-is and then write the output with the lines that aren't blank. Here's an example:

# Read lines as a list
fh = open("myfile", "r")
lines = fh.readlines()
fh.close()

# Weed out blank lines with filter
lines = filter(lambda x: not x.isspace(), lines)

# Write
fh = open("output", "w")
fh.write("".join(lines))
# should also work instead of joining the list:
# fh.writelines(lines)
fh.close()

Also, if you don't understand my lambda expression there, here's a longer route instead of using filter :

fh = open("myfile", "r")
lines = fh.readlines()
fh.close()

keep = []
for line in lines:
    if not line.isspace():
        keep.append(line)

fh = open("output", "w")
fh.write("".join(keep))
# should also work instead of joining the list:
# fh.writelines(keep)
fh.close()

All this is untested as I'm at work! But this should give you an idea of how to do it.
Hope that helps!

shadwickman
Posting Pro in Training
497 posts since Jul 2007
Reputation Points: 186
Solved Threads: 77
 

Actually, the empty lines in your data do not contain any spaces. Change the code a little to reflect this ...

# your data file read() ...
data_str = """\
>9|102396631|genome.....

CCACTTTCTCTCCGCGCTGGGTTGAACATGGTACATCAGACTCCCTGAATCTGTCAGATC

TCTTGGTTGCTGTTGACAACTAAGACTTCGGAGCCTGGAG_GATTTGTTGAGGGGGAGCA

GTTCTGGGTGTCCTCTGCTGTGACTGGGCTCCCGGCCTCTTGATAATGCGCAACAGCTGC

GGTAGAACATCTCATTCCCAG

>9|102362951|ENSRNOS....
"""

# create a list of lines
# or get the list directly with data file readlines()
mylist = data_str.split('\n')

new_str = ""
for item in mylist:
    if item:
        new_str += item + '\n'

print(new_str)

"""
my output -->
>9|102396631|genome.....
CCACTTTCTCTCCGCGCTGGGTTGAACATGGTACATCAGACTCCCTGAATCTGTCAGATC
TCTTGGTTGCTGTTGACAACTAAGACTTCGGAGCCTGGAG_GATTTGTTGAGGGGGAGCA
GTTCTGGGTGTCCTCTGCTGTGACTGGGCTCCCGGCCTCTTGATAATGCGCAACAGCTGC
GGTAGAACATCTCATTCCCAG
>9|102362951|ENSRNOS....
"""
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks a lot ...:)

joe82
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You