In: Computer Science
How would I get rid of punctuation that's in a file so that I am only left with words after turning them into a list in Python. So if the file contained "and in the dream, I am flying.", how would I get a list that contained ['and', 'in', 'that', 'dream', 'I', 'am', 'flying'] not ['and', 'in', 'that', 'dream,', 'I', 'am', 'flying.']
Explanation:
Lets say the filename is file.txt, containing the sentence given above.
First all the data from the file is read using the read() method inside a variable named data.
After that, all the words are splitted inside the list words, currently along with punctuations.
now each word is traversed in the list, and any punctuations at the starting or ending of any word is removed.
Code:
f = open("file.txt", 'r')
data = f.read()
words = data.strip().split(" ")
for i in range(len(words)):
if words[i][0].isalpha()!=True:
words[i] = words[i][1:]
if(words[i][-1].isalpha()!=True):
words[i] = words[i][:-1]
print(words)
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!