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.
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.