Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
def fileinput():
    with open('constant.txt') as f:
        lines = f.readlines()
        print(lines)

    print('Initial string: ', lines)
    res = ''.join([i for i in lines if not i.isdigit()])
    print('Final string: ', res)


fileinput()


What I have tried:

['Geeks123for127Geeks']
Initial string:  ['Geeks123for127Geeks']
Final string:  Geeks123for127Geeks


This is the output that I am getting. The problem is for 'Final string:' I need it to display the string without the integers. How would I fix this?
Posted
Updated 22-Nov-22 22:23pm

1 solution

Your code is iterating over the lines of the file, and testing whether the entire line is a digit. You need to remove the digits from each line instead.

Try something like this:
Python
def removeDigits(str):
    return str.translate({ord(i): None for i in '0123456789'})

def fileinput():
    with open('constant.txt') as f:
        lines = f.readlines()
        print(lines)
    
    print('Initial string: ', lines)
    res = list(map(removeDigits, lines))
    print('Final string: ', res)

fileinput()
 
Share this answer
 
Comments
CPallini 23-Nov-22 4:33am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900