This is what I've tried so far:
import re
string = open('old.txt').read()
new_str = re.sub(r'[A-Z]', '', string)
characters_to_remove = '!- '
for character in characters_to_remove:
new_str = new_str(character, '')
open('new.txt', 'w').write(new_str)
It doesn't work, can you explain why?
CodePudding user response:
Should work.
This code will check with is_alpha if the i character is an alphabetic letter (A-Z, case unsensitive) and if it returns false, will add it to the new_string variable.
Later, it will check if the string contains the characters to remove.
string = open('old.txt').read()
characters_to_remove = '!- '
new_str = ''.join(i for i in string if not i.isalpha())
for c in characters_to_remove:
new_str = new_str.replace(c, "")
open('new.txt', 'w').write(new_str)
Check information for is_alpha.
CodePudding user response:
I understand that you would like to remove every character except:
- 0,1,2,3,4,5,6,7,8,9
- .
- :
Here you go:
import re
with open(r'old.txt','r') as oldFile,open(r'new.txt', 'w') as newFile:
newFile.write(re.sub("[^0-9.:\n] ","",oldFile.read()))
