Question

In: Computer Science

How would I get rid of punctuation that's in a file so that I am only...

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.']

Solutions

Expert Solution

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!


Related Solutions

I am confused about how the pattern and how to get only some letters that are...
I am confused about how the pattern and how to get only some letters that are needed and numbers? New Perspectives HMTL5, CSS3, and JavaScript Chapter 13 Case Problem 2 HTML Document Scroll down to the empInfo table and add pattern attributes to the following fields using regular expressions (Note: Regular expressions in the HTML pattern attribute do not include the opening and closing / character): The accID field should consist only of the letters “ACT” followed by exactly 6...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
Paragraph on how to get rid of Gender inequality in the workplace
Paragraph on how to get rid of Gender inequality in the workplace
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
i am working on a project.. i would love to implement ENERGY METERING WITH IT SO...
i am working on a project.. i would love to implement ENERGY METERING WITH IT SO AS TO BE ABLE TO CALCULATE THE BILLS AT THE END OF A CERTAIN PERIOD.. how could this be done...BULBS,FANS,SOCKETS are the appliances involved... how can i get the energy used by all this appliances. i need very DETAILED TECHNICAL EXPLANATION.
I am so lost on this. I am making a B in this class but I...
I am so lost on this. I am making a B in this class but I can not seem to get this project completed. Please help. ACC-120 Project – Financial Accounting This assignment supports the following outcomes: ·          Prepare journal and ledger entries for a service or merchandising business. ·          Prepare year-end statements for a service or merchandising business. ·          Report cost decisions using accounting principles and financial statement analysis. ·          Evaluate how knowledge, skills, and attitudes learned in this...
1.Would it be better to just lower the cost of transit than to get rid of...
1.Would it be better to just lower the cost of transit than to get rid of it entirely? 2.if you couldn’t afford to pay the fare price would you do fare evasion? 3.Does the u.s not care if certain countries are crying for help? 4. Why does hurricane Katrina alway happen now?
I am not quite sure how to get this program working or how to start it...
I am not quite sure how to get this program working or how to start it in the first place. Write a full Java program and do the following: 1- Create a generic class and declare a one dim array as a private member. 2- Add a constructor to initialize the array. 3- A set method to set the array. 4- Sort method to sort the array. 5- Print method to print the array. 6- Reverse method to reverse the...
I understand that in nature wind would never get high enough, but I am just curious...
I understand that in nature wind would never get high enough, but I am just curious as to whether physics would allow this to occur or not.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT