Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove lines that does not end with numbers?

I have a text file Mytext.txt that looks like this,

0 1 A
1 2 T
2 3 A
3 4 B
4 5 A
5 6  
6 7 A
7 8 D
8 9 C
9 10  
10 11 M
11 12 Z
12 13 H

What is the easiest way in python with which I can remove the lines that do not end with a letter? So the above becomes

0 1 A
1 2 T
2 3 A
3 4 B
4 5 A
6 7 A
7 8 D
8 9 C
10 11 M
11 12 Z
12 13 H

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

with open('Mytext.txt', 'r') as fin:
    with open('Newtext.txt', 'w') as fout:
        for line in fin:
            if line.rstrip()[-1].isalpha()
                fout.write(line)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading