Let's say I have a file called input.txt that looks like
I listened to 4 u2 albums today
meet me at 5
squad 4ever
I want to filter out numbers that are on their own, so "4" and "5" should go, but "u2" and "4ever" should remain unchanged. the conclusion should be
I listened to u2 albums today
meet me at
squad 4ever
I tried to use this code
for line in fileinput.input("input.txt", inplace=True):
new_s = ""
for word in line.split(' '):
if not all(char.isdigit() for char in word):
new_s += word
new_s += ' '
print(new_s, end='')
Which is very similar to the code I found here: Removing numbers mixed with letters from a string
But instead of the desired output, I get
I listened to u2 albums today
meet me at 5
squad 4ever
As you can see, there are two problems: first, only the first line loses the number I want to lose, "5" is still present in the second line. The second problem is an extra space at the beginning of a new line.
stackoverflow, , . - ?