Search code examples
pythontext

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

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)