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 can I remove element from list?

Attempts to remove characters from the string that are not numbers. Unfortunately, I cannot do this due to a reason. I also tried "del file[i][j]", unfortunately without.

Code:

file_open = open("napisy.txt","r")
wyniki = open("wyniki4.txt","w")
file = file_open.read().split()
file1 = file
print(file)
cyfry = ['0','1','2','3','4','5','6','7','8','9']
cyfry_zad3 = ''
licznik_cyfr = 0
tab = []
for i in range(0,len(file1)):
    for j in range(0,len(file1[i])):
        if file1[i][j] not in cyfry:
            file1.remove(file1[i][j])
            #del file1[i][j]
print(file1)

result

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

enter image description here

>Solution :

You can use this

file_open = open("napisy.txt","r")
file = file_open.read()
file_open.close()

for char in list(file):
    if char not in "1234567890":
        file = file.replace(char, "")
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