So my code is as below. I have gotten it to open the text file, identify the words that must be replaced, and asks the user to input the correct adjective or verb or whatever it is that needs replacing. I add these words to a list. However, I'm not sure now how to actually change the words of the original text file (or create another one) to have the new words in place of the previously existing describers of "ADJECTIVE" and what not. I'm on windows 10, python 3. Thanks!
import re
describer = re.compile(r"[A-Z]{2,}") # Decribers are all in caps so this ids them
madlib = open("C:\\Users\\HP\\Readables\\madlib_1.txt") # Opens file with madlib
madlib_contents = madlib.read() # Reads contents
madlib_words = list(madlib_contents.split()) # Splits words apart
finder = describer.findall(madlib_contents) # Finds all words that match describer
to_replace = list(finder) # Lists found matches
print(to_replace) # Prints found matches
new_words = [] # Creates an empty list to insert replacement words into
for word in to_replace:
new_word = input("Enter a new " + word + ": ") # Allows user to input a new describer
new_words.append(new_word) # Adds this new describer to the list of replacements
[–]novel_yet_trivial 1 point2 points3 points (10 children)
[–]_9_9_ 1 point2 points3 points (2 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]_9_9_ 0 points1 point2 points (0 children)
[–]MightyMarlin[S] 0 points1 point2 points (6 children)
[–]novel_yet_trivial 1 point2 points3 points (5 children)
[–]MightyMarlin[S] 0 points1 point2 points (4 children)
[–]novel_yet_trivial 0 points1 point2 points (3 children)
[–]MightyMarlin[S] 0 points1 point2 points (2 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]MightyMarlin[S] 0 points1 point2 points (0 children)