Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is using python I want to change the text weather to whether so the text is

ID: 3596913 • Letter: T

Question

This is using python I want to change the text weather to whether

so the text is like this

weather not good

there ismany other weather etc

----------------------------------------

i want to change to

whether not good

--------------------------------------

so the first loop i notice that it is a long string so the python cant recognize the string how do i break it up so it notice by word

------------------------------------

textin = open('weather.txt','r')
textout= open ('newtext.txt','w')
newtext=""
swap= ""

for word in textin:
if word == "weather":
swap = word.replace("weather","whether")

else:
swap = word
newtext+=swap

Explanation / Answer

f1 = open('weather.txt', 'r')#this will not load the data in file, it will only return a handle to read t2 = open('newtext.txt', 'w') data = f1.read()#now we have read all the text in file weather.txt and it is stored in data newtext = "" swap = "" textin = data.split(' ')#we break up the text into words separated by spaces for word in textin: if word == "weather": swap = word.replace("weather", "whether") elif word == "Weather": swap = word.replace("Weather", "Whether") else: swap = word newtext += swap + " " t2.write(newtext)#write the new text in file specified at t2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote