automate-boring-stuff/exercises/ex09/ex09b.py

22 lines
479 B
Python

"""
Project: Mad Libs
Page 230
"""
import re
import sys
from pathlib import Path
keywords = ["ADJECTIVE", "NOUN", "ADVERB", "VERB"]
infile = Path(sys.argv[1])
outfile = infile.parent / f"{infile.stem}.res{infile.suffix}"
text = infile.read_text()
matches = re.finditer(r'(' + r'|'.join(keywords) + r')', text)
for match in matches:
name = match.group()
res = input(f"Enter {name.lower()}:\n")
text = text.replace(name, res, 1)
print(text)
outfile.write_text(text)