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

This is using python. How to make this code work without using .split() i am con

ID: 3598740 • Letter: T

Question

This is using python.

How to make this code work without using .split() i am confuse that my teacher said I cant use split(). this is the the old input:

Weather the whether be fine,

Or weather the whether be not.

Weather the whether be cold,

Or weather the whether be hot.
We'll whether the whether,

Whatever the whether,

Weather we like it or not.

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

this is after running the porgram the new file should be like this:

Whether the weather be fine, Or whether the weather be not. Whether the weather be cold, Or whether the weather be hot. We'll weather the whether, Whatever the whether, Whether we like it or not.

Whether the weather be fine,
Or whether the weather be not.
Whether the weather be cold,
Or whether the weather be hot.
We'll weather the weather,
Whatever the weather,
Whether we like it or not.

f1 = open("weather.txt', 'r')#this will not load t t2 = open(newtext.txt','w') data = f1.read()#now we have read all the text in newtext "" swap "" textin = data, split(' ') for word in textin: if word == "weather": elif word =- "Weather". elif word == "whether". elif word"Whether": else: swap word . replace("weather", "whether") = swap word. replace("Weather", "Whether") = word.replace( "whether", "weather") word.replace("Whether", "Weather") swap word net ext += swap + print (swap) print (newtext)

Explanation / Answer

import re
f1 = open("weather.txt", 'r')
t2 = open('newtext.txt', 'w')

data = f1.read()

newtext = ""
swap = ""
for word in re.findall(r'[a-zA-z']+', data):
if word == "weather":
swap = word.replace("weather", "whether")
elif word == "Weather":
swap = word.replace("Weather", "Whether")
if word == "whether":
swap = word.replace("whether", "weather")
elif word == "Whether":
swap = word.replace("Whether", "Weather")
else:
swap = word
newtext += swap + " "
print(swap)
print(newtext)

Regex can also be used to tokenize.

# copy pastable link: https://paste.ee/p/Xsb65

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