Reputation: 9
I have a text file file1.txt. I have already performed preprocessing (cleaning) on it. Operations like replace("-","") etc. Now I want to perform few more operations on the clean text string. How can I check if the string is cleaned or not i.e without any special characters like @!#$%^ etc.? If not cleaned it should print invalid entry? Can I do it without using re?
def process():
with open("file1.txt",'r') as a:
text = a.read #toconvert into string
for ch in ['_','-']:
text = text.replace(ch,"")
return text
process()
Upvotes: 1
Views: 189
Reputation: 4487
Without using regex, you can do this:
special_char = '@!#$%^' # maybe string.punctuation?
text ='I love pizza and chips@'
print("Invalid") if any(c in special_char for c in text) else print("Valid")
To answer your second question, you should write in your case:
ch = '@#$%^&*'
with open(cleanfile, 'r') as s:
s = s.readlines()
if any(c in ch for c in s):
print('Invalid')
else:
li = s.split()
Upvotes: 2