starting from a file text with characters like "!" or "," (basically, the entire set of string.punctuation) i want to remove them and obtain a text with only all words. Here i found a solution: https://gomputor.wordpress.com/2008/09/27/search-replace-multiple-words-or-characters-with-python/ and i wrote the script in this way:
import string
dict={}
for elem in string.punctuation:
dict[elem]=""
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
with open ("text.txt","r") as f:
file = f.read()
f = replace_all(file,dict)
print(f)
ok this works, but if i try this another solution, i will not have the same result:
with open ("text.txt","r") as f:
file = f.read()
for elem in string.punctuation:
if elem in file:
f=file.replace(elem,"")
In this case, if i typing print(f) i have exactly the same file with all punctuations. Why?