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

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!

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....
"""

Thanks a lot ...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.