Question

In: Computer Science

Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...

Python Coding Question (Data Science)

Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want the word "happy" to get replaced with a +1 in the Practice_forhw1.txt file.

My poor attempt below:

pos_file = open("Positive.txt", 'r')
#positive words are now in a list
pos_list = pos_file.readlines()
p_file = open("Practice_forhw1.txt", 'r')
p_list = p_file.readlines()


for word in p_list:
if word in p_list == word in pos_list:
word = word.replace(word, '+1')
print (word)

Solutions

Expert Solution

If you have any doubts, please give me comment...

pos_file = open("Positive.txt", 'r')

#positive words are now in a list

# striping newlines and carriage returns

pos_list = [line.strip() for line in pos_file.readlines()]

pos_file.close()

p_file = open("Practice_forhw1.txt", 'r')

# striping newlines and carriage returns

p_list = [line.strip() for line in p_file.readlines()]

p_file.close()

for word in pos_list:

    if word in p_list:

        pos = p_list.index(word)

        if pos!=-1:

            p_list[pos] = "+1"

# restoring elements to file

fout = open("Practice_forhw1.txt", 'w')

print('\n'.join(p_list), file=fout)

fout.close()


Related Solutions

Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I...
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I am confused on what they're asking :             Choose a stock that interests you. Utilizing Bloomberg (or other financial websites) as a source of data, collect the following      information:                         a. The stock’s Beta                         b. The rate of return on the market (S&P 500 Index)                         c. The risk-free rate (rRF)                         d. The last dividend paid (D0)...
Hello, I am having difficulty with these questions. Please provide answers to all parts and show...
Hello, I am having difficulty with these questions. Please provide answers to all parts and show some work so I can learn. Thank you so much in advance! :) Suppose that you began a one-year study of tuberculosis (TB) in a subsidized housing community in the Lower East Side of New York City on January 1st, 2016. You enrolled 500 residents in your study and checked on their TB status on a monthly basis. At the start of your study...
Hello, I am just having a hard time understanding this question. The question is below. I...
Hello, I am just having a hard time understanding this question. The question is below. I am not supposed to write a program but instead in regular english sentence form just name the methods, fields, and variables I would use for this loan class. Im not really sure what a loan class means as well, thank you for your help. USING JAVA Given a Loan class, name the necessary fields and methods that we would need in this class. Be...
hello, I am having an issue with a question in my highway engineering course. the question...
hello, I am having an issue with a question in my highway engineering course. the question is: An equal tangent sag vertical curve has an initial grade of –2.5%. It is known that the final grade is positive and that the low point is at elevation 82 m and station 1 + 410.000. The PVT of the curve is at elevation 83.5 m and the design speed of the curve is 60 km/h. Determine the station and elevation of the...
Adding code that will keep cumulative count of characters? Hello, I am having issues in figuring...
Adding code that will keep cumulative count of characters? Hello, I am having issues in figuring out how to keep letter from a .txt file with the declaration of independence, this is my code, I can keep track of the frequency but can't figure out the way to keep adding the total characters, as the you go down the alphabet the number is supposed to be counting up #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #define DEBUG void main() {   ...
I am currently having trouble understanding/finding the errors in this python code. I was told that...
I am currently having trouble understanding/finding the errors in this python code. I was told that there are 5 errors to fix. Code: #!/usr/bin/env python3 choice = "y" while choice == "y": # get monthly investment monthly_investment = float(input(f"Enter monthly investment (0-1000):\t")) if not(monthly_investment > 0 and monthly_investment <= 100): print(f"Entry must be greater than 0 and less than or equal to 1000. " "Please start over.")) #Error 1 extra ")" continue # get yearly interest rate yearly_interest_rate = float(input(f"Enter...
I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
Hi there, I am having difficulty with the attached homework question. If you guys could please...
Hi there, I am having difficulty with the attached homework question. If you guys could please help work through the steps. My balance sheet isn't balancing :/ These financial statement items for Fairview Corporation at year-end, July 31, 2017. Salaries and wages payable $2,080 Salaries and wages expense 57,500 Supplies expense 15,600 Equipment 18,500 Accounts payable 4,100 Service revenue 66,100 Rent revenue 8,500 Notes payable (due in 2020) 1,800 Common stock 16,000 Cash 29,200 Accounts receivable 9,780 Accumulated depreciation-equipment 6,000...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
This assignment is For a C++ class , I am having difficulty with getting the boolean...
This assignment is For a C++ class , I am having difficulty with getting the boolean statements the most; getting them set up properly inside the int main() area. You have been asked to define a tip-calculating function named calcTip that could be integrated into a larger piece of software designed to improve guest service at a restaurant. As a result, you must conform to the function specification used throughout the rest of the project's code: double calcTip(double checkAmount, bool...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT